联盟二在C#中一起列出

时间:2011-02-21 17:01:18

标签: c# .net linq union

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; }
    }

如何使我的SelectListItem类IEquitable?

上面的代码应该解释我想在评论中做些什么。有人告诉我,我可以使用LINQ,但我不知道在LINQ方面我在做什么。谢谢!

3 个答案:

答案 0 :(得分:3)

你的意思是这样吗?

return new SelectList(oldSelectList.Union(newSelectList), "Value", "Text");

答案 1 :(得分:3)

// 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");

答案 2 :(得分:0)

return new SelectList(newSelectList.Concat(newSelectList), "Value", "Text");