简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM