如何编写通用 IEnumerable扩展方法

发布于 2024-08-23 14:39:25 字数 975 浏览 13 评论 0原文

我对仿制药相当陌生(好吧,真的很新),但我喜欢它们的想法。我将在视图上有一些下拉列表,我想要一种通用的方法来获取对象列表并将其转换为 SelectListItems 列表

我现在拥有的:

public static IEnumerable<SelectListItem> ToSelectListItems(
     this IEnumerable<SpecificObject> items, long selectedId)
    {
        return
            items.OrderBy(item => item.Name)
            .Select(item =>
                    new SelectListItem
                    {
                        Selected = (item.Id == selectedId),
                        Text = item.Name,
                        Value = item.Id.ToString()
                    });
    }

麻烦是,我需要为每个下拉列表重复该代码,因为对象具有代表 SelectListItemText 属性的不同字段,

这就是我想要的完成:

public static IEnumerable<SelectListItem> ToSelectListItem<T>(this IEnumerable<T> items, string key, string value, int SelectedId) {
    // I really have no idea how to proceed from here :(
}

I'm fairly new (ok, REALLy new) to generics but I love the idea of them. I am going to be having a few drop-down lists on a view and I'd like a generic way to take a list of objects and convert it to a list of SelectListItems

What I have now:

public static IEnumerable<SelectListItem> ToSelectListItems(
     this IEnumerable<SpecificObject> items, long selectedId)
    {
        return
            items.OrderBy(item => item.Name)
            .Select(item =>
                    new SelectListItem
                    {
                        Selected = (item.Id == selectedId),
                        Text = item.Name,
                        Value = item.Id.ToString()
                    });
    }

Trouble is, I'd need to repeat that code for each drop-down as the objects have different fields that represent the Text property of the SelectListItem

Here is what I'd like to accomplish:

public static IEnumerable<SelectListItem> ToSelectListItem<T>(this IEnumerable<T> items, string key, string value, int SelectedId) {
    // I really have no idea how to proceed from here :(
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌能量女王 2024-08-30 14:39:26

您可以传入委托来进行比较和属性检索。像这样:

public static IEnumerable<SelectListItem> ToSelectListItem<T>(this IEnumerable<T> items, 
    int selectedId, Func<T,int> getId, Func<T, string> getName, 
    Func<T, string> getText, Func<T, string> getValue)
{
    return
        items.OrderBy(item => getName(item))
        .Select(item =>
                new SelectListItem
                {
                    Selected = (getId(item) == selectedId),
                    Text = getText(item),
                    Value = getValue(item)
                });
}

然后你会像这样使用它:

var selected = specificObjects.ToSelectListItem(10, s => s.Id, s=> s.Name, s => s.Name, s => s.Id.ToString());

You could pass in delegates to do the comparisons, and property retrieval. Something like this:

public static IEnumerable<SelectListItem> ToSelectListItem<T>(this IEnumerable<T> items, 
    int selectedId, Func<T,int> getId, Func<T, string> getName, 
    Func<T, string> getText, Func<T, string> getValue)
{
    return
        items.OrderBy(item => getName(item))
        .Select(item =>
                new SelectListItem
                {
                    Selected = (getId(item) == selectedId),
                    Text = getText(item),
                    Value = getValue(item)
                });
}

Then you would use it like so:

var selected = specificObjects.ToSelectListItem(10, s => s.Id, s=> s.Name, s => s.Name, s => s.Id.ToString());
滿滿的愛 2024-08-30 14:39:26

为了使其按照书面方式工作,您的类型 T 将需要实现一些提供 NameId 属性的接口:

 public interface ISelectable
 {
     string Name { get; }
     int Id { get; }
 }

有了这个,您可以执行以下操作:

public static IEnumerable<SelectListItem> ToSelectListItems<T>(this IEnumerable<T> items, long selectedId) 
     where T : ISelectable
{
    return
        items.OrderBy(item => item.Name)
        .Select(item =>
                new SelectListItem
                {
                    Selected = (item.Id == selectedId),
                    Text = item.Name,
                    Value = item.Id.ToString()
                });
}

这是为了在扩展方法中使用 NameId 属性所必需的...相反,您可以提供一种不同的方式来接收这些属性(即:传递代表),但这可能会也可能不会增加您场景中的维护成本。

In order for this to work as written, your type T will need to implement some interface which provides Name and Id properties:

 public interface ISelectable
 {
     string Name { get; }
     int Id { get; }
 }

With this in place, you can do:

public static IEnumerable<SelectListItem> ToSelectListItems<T>(this IEnumerable<T> items, long selectedId) 
     where T : ISelectable
{
    return
        items.OrderBy(item => item.Name)
        .Select(item =>
                new SelectListItem
                {
                    Selected = (item.Id == selectedId),
                    Text = item.Name,
                    Value = item.Id.ToString()
                });
}

This is required in order to use the Name and Id properties within your extension method... You could, instead, provide a different means of receiving these (ie: passing delegates), but that may or may not increase the maintenance cost in your scenario.

各空 2024-08-30 14:39:26

//需要使用反射来获取值

   public static IEnumerable<SelectListItem> GetListItems<T> 
     (this IEnumerable<T> items, int selectedVal)
    {
        return from item in items
               select new SelectListItem
               {
                   Text =item.GetPropValue("Name"),
                    Value = item.GetPropValue("Id"),
                     Selected = 
                item.GetPropValue("Id").Equals(selectedVal.ToString())
               };
    }

    public static string GetPropValue<T> (this T item,string refName)
    {

        return 
           item.GetType().GetProperty(refName).GetValue(item).ToString();
    }

//You need to use reflection to get the value of

   public static IEnumerable<SelectListItem> GetListItems<T> 
     (this IEnumerable<T> items, int selectedVal)
    {
        return from item in items
               select new SelectListItem
               {
                   Text =item.GetPropValue("Name"),
                    Value = item.GetPropValue("Id"),
                     Selected = 
                item.GetPropValue("Id").Equals(selectedVal.ToString())
               };
    }

    public static string GetPropValue<T> (this T item,string refName)
    {

        return 
           item.GetType().GetProperty(refName).GetValue(item).ToString();
    }
坏尐絯℡ 2024-08-30 14:39:25

好吧,你可以这样做:

public static IEnumerable<SelectListItem> ToSelectListItems(
     this IEnumerable<T> items, 
     Func<T,string> nameSelector, 
     Func<T,string> valueSelector, 
     Func<T,bool> selected)
{
     return items.OrderBy(item => nameSelector(item))
            .Select(item =>
                    new SelectListItem
                    {
                        Selected = selected(item),
                        Text = nameSelector(item),
                        Value = valueSelector(item)
                    });
}

Well, you could do something like this:

public static IEnumerable<SelectListItem> ToSelectListItems(
     this IEnumerable<T> items, 
     Func<T,string> nameSelector, 
     Func<T,string> valueSelector, 
     Func<T,bool> selected)
{
     return items.OrderBy(item => nameSelector(item))
            .Select(item =>
                    new SelectListItem
                    {
                        Selected = selected(item),
                        Text = nameSelector(item),
                        Value = valueSelector(item)
                    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文