【发布时间】:2019-10-25 00:32:26
【问题描述】:
我希望将两个具有相似代码但变量类型不同的函数结合起来。我想在IEnumerable 中使用 T 类型,但它似乎不起作用。
方法一:
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;
}
方法二:
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<Make> 和IList<ListItem> 的返回类型完全相同,后者是在IEnumerable<Make> 和IEnumerable<ListItem> 的参数类型上传递的。
【问题讨论】: