c# - Find missing information between 2 lists -
how can find missing information between 2 different lists in
the code generates lists
private static ienumerable<user> getsomeusers() { var mikesgroups = new list<string> { "group1", "group2" }; var mike = new user { username = "mike", memberof = mikesgroups }; var davidsgroups = new list<string> { "group3", "group1" }; var david = new user { username = "david", memberof = davidsgroups }; return new list<user> { mike, david }; } private static ienumerable<group> getgroups() { var group1users = new list<string> { "mike", "david", "kim" }; var group1 = new group { name = "group1", members = group1users }; var group2users = new list<string> { "mike", "david","kim" }; var group2 = new group { name = "group2", members = group2users }; return new list<group> { group1, group2 }; }
entities:
public class user { public string username { get; set; } public ilist<string> memberof { get; set; } // list of group names } public class group { public string name { get; set; } public ilist<string> members { get; set; } // list of username }
result should 2 lists (missing groups , missing users)
group3
missing in group list , linked user"david"
kim
missing in user list , linkedgroup1
,group2
2 dictionaries must returned key = missing value = foundin
dictionary<string,ilist<string>> missingusers; item 1 > key="kim", value={"group1","group2"} dictionary<string,ilist<string>> missinggroup; item 1 > key="group3",{"david"}
edit=>
i manged list of missing not linked (list of values in dictionary)
var missinggroups = users.selectmany(g => g.memberof).tolist().except(groups.select(w => w.name)); var missingusers= groups.selectmany(g => g.members).tolist().except(users.select(u => u.username));
ok, found solution. please feel free correct if found better 1 has better performance.
var missinggroup = users.todictionary(user => user.username, user => user.memberof.except(groups.select(w => w.name))) .where(f => f.value != null && f.value.count() > 0) .todictionary(x => x.key, x => x.value);
Comments
Post a Comment