c# - Can I merge these two lists in 1 go -
what trying do,
// members sharepoint list (can null) // members database (can null) // merge database members sharepoint list members database members should have property vip = true // merge mean if not in list add them list, if in list change there property vip = true // default vip property false
what have developed far,
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 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();
problem
can use linq , merge these lists in single go ? maybe this, pseudo be
var dbmembers = //getdbmembers active or pending var spmembers = select members using `.cast<splistitem>()` if spmembers has dbmember (compared email) change spmembers vip property true (which default false) rest dbmembers doesn't exists in spmembers, add them vip property = true
not sure how can efficiently put above pseudo code linq
try this:
var allspmembers = getsplist(); // members mentioned before `.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 mapping here splistitem model } yoursplistitem.vip = true; return yoursplistitem; }).tolist();
Comments
Post a Comment