How to reorganize these two similar methods into one?

I saw several examples of using "T" to make the method reusable for general collections of different classes, but I never got into it or understood the patterns.

I wonder if it is possible to put 2 methods below into one and what could mean that it will (in terms of performance).

Is anyone

        [NonAction]
        public List<SelectListItem> ToSelectList(IEnumerable<Department> departments, string defaultOption)
        {
            var items = departments.Select(d => new SelectListItem() { Text = d.Code + " - " + d.Description, Value = d.Id.ToString() }).ToList();
            items.Insert(0, new SelectListItem() { Text = defaultOption, Value = "-1" });
            return items;
        }

        [NonAction]
        public List<SelectListItem> ToSelectList(IEnumerable<Function> functions, string defaultOption)
        {
            var items = functions.Select(f => new SelectListItem() { Text = f.Description, Value = f.Id.ToString() }).ToList();
            items.Insert(0, new SelectListItem() { Text = defaultOption, Value = "-1" });
            return items;
        }

Decision

The solution I used:

Using

var departmentItems = departments.ToSelectList(d => d.Code + " - " + d.Description, d => d.Id.ToString(), " - ");
var functionItems = customerFunctions.ToSelectList(f => f.Description, f => f.Id.ToString(), " - ");

from

 public static class MCVExtentions
    {
        public static List<SelectListItem> ToSelectList<T>(this IEnumerable<T> enumerable, Func<T, string> text, Func<T, string> value, string defaultOption)
        {
            var items = enumerable.Select(f => new SelectListItem() { Text = text(f), Value = value(f) }).ToList();
            items.Insert(0, new SelectListItem() { Text = defaultOption, Value = "-1" });
            return items;
        }
    }
+5
source share
3 answers

Without implementing a generic interface like @Grzenio, you can use a generic method like this:

    public List<SelectListItem> ToSelectList<T>(IEnumerable<T> enumerable, Func<T, string> text, Func<T, string> value, string defaultOption)
    {
        var items = enumerable.Select(f => new SelectListItem() { Text = text(f), Value = value(f) }).ToList();
        items.Insert(0, new SelectListItem() { Text = defaultOption, Value = "-1" });
        return items;
    }

    // use like

    t.ToSelectList(departments, d => d.Code + " - " + d.Description, d => d.Id.ToString(), "default");
    t.ToSelectList(functions, f => f.Description, f => f.Id.ToString(), "default");
+7
source

- , :

interface A
{
int ID{get;}
string Description{get;}
}

d.Code + " - " + d.Description. :

[NonAction]
    public List<SelectListItem> ToSelectList(IEnumerable<A> as, string defaultOption)
    {
        var items = as.Select(a => new SelectListItem() { Text = a.Description, Value = a.Id.ToString() }).ToList();
        items.Insert(0, new SelectListItem() { Text = defaultOption, Value = "-1" });
        return items;
    }

: , ,

  • , ,
  • ,
+8

, - ( ).

[NonAction]
public List<SelectListItem> ToSelectList<T>(IEnumerable<T> en, 
                                            Function<string, T> text, 
                                            Function<string, T> value, 
                                            string defaultOption)
{
    var items = en.Select(x => new SelectListItem() { Text = text(x) , Value = value(x) }).ToList();
    items.Insert(0, new SelectListItem() { Text = defaultOption, Value = "-1" });
    return items;
}

( ).

[NonAction]
public List<SelectListItem> ToSelectList(IEnumerable<Department> departments, 
                                         string defaultOption)
{
    return ToSelectList<Department>(departments, d =>  d.Code + '-' + d.Description, d => d.Id.ToString(), defaultOption);

}
+4

All Articles