[英]Union two lists together in C#
public static SelectList IndicationsGroup(int? entityId, int? projectType, int? oldProjectType)
{
List<SelectListItem> oldSelectList = new List<SelectListItem>();
List<SelectListItem> newSelectList = new List<SelectListItem>();
ISpWeb_ProjectListResultSet newList = Chatham.Web.Proxies.TransactionsDataTier.ExecSpWeb_ProjectList(entityId, projectType);
ISpWeb_ProjectListResultSet oldList = Chatham.Web.Proxies.TransactionsDataTier.ExecSpWeb_ProjectList(entityId, oldProjectType);
foreach (SpWeb_ProjectList1LightDataObject item in newList.SpWeb_ProjectList1)
{
newSelectList.Add(new SelectListItem() { Text = item.ProjectName, Value = item.ProjectName.GetHashCode().ToString() });
}
foreach (SpWeb_ProjectList1LightDataObject item in oldList.SpWeb_ProjectList1)
{
oldSelectList.Add(new SelectListItem() { Text = item.ProjectName, Value = item.ProjectName.GetHashCode().ToString() });
}
// I want to return a union of the two select lists...
// return new SelectList(unionedList, "Value", "Text");
}
public class SelectListItem
{
public SelectListItem();
public bool Selected { get; set; }
public string Text { get; set; }
public string Value { get; set; }
}
How would I make my SelectListItem class IEquitable? 我如何使我的SelectListItem类成为IEquitable?
The code above should explain what I want to do in the comment. 上面的代码应在注释中解释我想做什么。 Someone told me I could use LINQ but I have no idea what I'm doing when it comes to LINQ.
有人告诉我我可以使用LINQ,但是我不知道我在做什么。 Thanks!
谢谢!
你的意思是这样吗?
return new SelectList(oldSelectList.Union(newSelectList), "Value", "Text");
// This will return a "union ALL" of the two select lists...
return new SelectList(newSelectList.Concat(oldSelectList), "Value", "Text");
//this will return a union of distinct values of the two selects,
//PROVIDED that SelectListItem is IEquatable
return new SelectList(newSelectList.Union(oldSelectList), "Value", "Text");
//this will return a union of distinct values of the two selects,
//given an implementation of an IEqualityComparer<SelectListItem> equalityComparer
//that will semantically compare two SelectListItems
return new SelectList(newSelectList.Union(oldSelectList, equalityComparer), "Value", "Text");
return new SelectList(newSelectList.Concat(newSelectList), "Value", "Text");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.