c# - Except() and Intersect() doesn't work correctly - LINQ -
this question has answer here:
- using linq except not working thought 3 answers
i not sure why not working correctly. possibly because trying use expressions on data class instead of single field in class?
adminonlygroups
supposed return groups admin has , user doesn't returning of admin groups
useronlygroups
doing same, returning of user groups
commongroups
supposed return groups have in common there 2 of, returning null or empty
data class
[datacontract] public class investigatorgroupdata { [datamember] public int investigatorgroupid { get; set; } [datamember] public string investigatorgroupname { get; set; } }
snippet of controller
ienumerable<investigatorgroupdata> admingroups = proxy.getinvestigatorgroups(adminid); ienumerable<investigatorgroupdata> usergroups = proxy.getinvestigatorgroups(userid); // groups admin has , user doesn't. these enabled , unselected ienumerable<investigatorgroupdata> adminonlygroups = admingroups.except(usergroups); // groups user has , admin doesn't. these disabled , selected ienumerable<investigatorgroupdata> useronlygroups = usergroups.except(admingroups); // groups in common. these enabled , selected ienumerable<investigatorgroupdata> commongroups = admingroups.intersect(usergroups); if (commongroups.isnullorempty()) { return view("error"); }
you need implement equality comparer objects
this has been answered before here using linq except not working thought
please see blog post below msdn further explanation http://blogs.msdn.com/b/csharpfaq/archive/2009/03/25/how-to-use-linq-methods-to-compare-objects-of-custom-types.aspx
if anonymous types work differently without equality comparer. here example code
http://odetocode.com/blogs/scott/archive/2008/03/25/and-equality-for-all-anonymous-types.aspx
Comments
Post a Comment