用IEnumerable<T>

我想把两个代码相似但变量类型不同的函数组合起来。我想在IEnumerable中使用T类型的变量。IEnumerable 但似乎并不奏效。

方法一

public static IList<ListItem> AppendTopMakesToList(this IEnumerable<ListItem> options, bool appendSeparatorRow = true)
{
    if (options == null || string.IsNullOrEmpty(Sitecore.Configuration.Settings.GetSetting("RACQ.JoinMembership.Top10CarMakes"))) 
        return options.ToList();

    var topmakes = Sitecore.Configuration.Settings.GetSetting("RACQ.JoinMembership.Top10CarMakes").Split('|').ToList().ConvertAll(d => d.ToLower());
    var filteredMakes = options.Where(x => topmakes.Any(y => y.Contains(x.Text.ToLower()))).ToList();//getting all the makes from the listItems

    if (appendSeparatorRow)
    {
        var separatorListItem = new ListItem("------------------", "------------------", false);
        separatorListItem.Attributes.Add("disabled", "true"); //disabling the separator item so that it can't be selected
        filteredMakes.Add(separatorListItem);
    }

    var items = options.ToList();
    items.InsertRange(0, filteredMakes);

    return items;
}

方法2

public static IList<Make> AppendTopMakesToList(this IEnumerable<Make> options, bool appendSeparatorRow = true)
{
    if (options == null || string.IsNullOrEmpty(Sitecore.Configuration.Settings.GetSetting("RACQ.JoinMembership.Top10CarMakes"))) 
       return options.ToList();

    var topmakes = Sitecore.Configuration.Settings.GetSetting("RACQ.JoinMembership.Top10CarMakes").Split('|').ToList().ConvertAll(d => d.ToLower());
    var filteredMakes = options.Where(x => topmakes.Any(y => y.Contains(x.Code.ToLower()))).ToList();//getting all the makes from the listItems

    if (appendSeparatorRow)
    {
        var separatorListItem = new Make()
                {
                    Code = "------------------",
                    Description = "------------------"
                };

                filteredMakes.Add(separatorListItem);
            }

            var items = options.ToList();
            items.InsertRange(0, filteredMakes);

            return items;
        }
    }
}

这两个函数从返回类型IList和IList的参数类型IEnumberable和IEnumberable上传递的部分完全相同。

解决方案:

我会做这样的事情。

public static IList<T> AppendItems<T>(this IEnumerable<T> options, Action<T> blankFactory, bool appendSeparatorRow = true)
        {
            if (options == null || string.IsNullOrEmpty(Sitecore.Configuration.Settings.GetSetting("RACQ.JoinMembership.Top10CarMakes"))) return options.ToList();
            var topmakes = Sitecore.Configuration.Settings.GetSetting("RACQ.JoinMembership.Top10CarMakes").Split('|').ToList().ConvertAll(d => d.ToLower());
            var filteredMakes = options.Where(x => topmakes.Any(y => y.Contains(x.Code.ToLower()))).ToList();//getting all the makes from the listItems
            if (appendSeparatorRow)
            {
                var separatorListItem = blankFactory();
                filteredMakes.Add(separatorListItem);
            }
            var items = options.ToList();
            items.InsertRange(0, filteredMakes);
            return items;
        }

然后你可以像这样使用它:

var whatever = new List<ListItem>():


AppendItems(whatever, () => {
                var separatorListItem = new ListItem("------------------", "------------------", false);
                separatorListItem.Attributes.Add("disabled", "true");
    return separatorListItem;

}, true);

给TA打赏
共{{data.count}}人
人已打赏
解决方案

可观察订阅响应对象范围

2022-5-14 8:00:38

解决方案

在谷歌云平台上启动python gmailAPI脚本时的认证问题。

2022-5-14 9:00:07

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索