我想做的是

// get members from SharePoint list (can be null)
// get members from database (can be null)
// merge database members with sharepoint list members BUT only database members should have property VIP = true
// by merge I mean if they are not in list then add them to list, if they are in list then just change there property VIP = true
// by default VIP property is false


我到目前为止的发展

List<Member> Members = new List<Member>();
        foreach (SPListItem mItem in GetList(Url).Items)
        {
            Member m = new Member();
            m.ID = mItem.ID;
            m.Name = mItem.Title;
            m.Company = Utilities.ObjectToStringOrEmpty(mItem[companyCol]);
            m.eMail = Utilities.ObjectToStringOrEmpty(mItem[emailCol]);
            m.Comment = Utilities.ObjectToStringOrEmpty(mItem[commentCol]);
            m.Membership = Utilities.ObjectToStringOrEmpty(mItem[msCol]);
            Members.Add(m);
        }

        var cd = new MemberManager().GetMoreMembers(Url + "/");
        var activeMembers = cd.Where(am => am.MembershipStatus == "Active" || am.MembershipStatus == "Pending").ToList();
        if (activeMembers != null || activeMembers.Count() > 0)
        {
            foreach (var am in activeMembers)
            {
                if (!Members.Any(a => a.eMail.ToLowerInvariant() == am.Email.ToLowerInvariant()))
                {
                    Member m = new Member();
                    m.Name = am.FirstName + " " + am.LastName;
                    m.eMail = am.Email;
                    m.IsVip = true;
                    Members.Add(m);
                }
            }
        }

        md.Members = Members.ToArray();


问题

我可以使用Linq并一次性合并这些列表吗?也许像这样,伪会是

var dbMembers = //GetDBMembers that are active or pending

var spMembers =
              Select all members using `.Cast<SPListItem>()`
              If spMembers has any dbMember (compared by email)
              Then change that spMembers VIP property to true (which is by default false)
              For rest dbMembers that doesn't exists in spMembers, add them with VIP property = true


不知道如何将上述伪代码有效地放入linq

最佳答案

尝试这个:

var allSpMembers = GetSpList(); // get your members as you mentioned before by `.Cast<SPListItem>()`

List<SPListItem> spMembers =
    dbMembers.GroupJoin(allSpMembers, dbM => dbM.Email, spM => spM.Email,
        (dbMember, spMember) => new { dbMember, spMember })
             .SelectMany(x => x.spMember.DefaultIfEmpty(), (x, spMember) =>
                 {
                     SPListItem yourSpListItem;

                     if (spMember != null)
                     {
                         yourSpListItem = spMember;
                     }
                     else
                     {
                         yourSpListItem = x.dbMember; //make some mapping here to SPListItem model
                     }

                     yourSpListItem.VIP = true;

                     return yourSpListItem;
                 }).ToList();

关于c# - 我可以一次性合并这两个列表吗,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33142542/

10-10 16:54