I have 2 list objects of type of some class,
class person { public string id { get; set; } public string name { get; set; } } List<person> pr = new List<person>(); pr.Add(new person { id = "2", name = "rezoan" }); pr.Add(new person { id = "5", name = "marman" }); pr.Add(new person { id = "3", name = "prithibi" }); List<person> tem = new List<person>(); tem.Add(new person { id = "1", name = "rezoan" }); tem.Add(new person { id = "2", name = "marman" }); tem.Add(new person { id = "1", name = "reja" }); tem.Add(new person { id = "3", name = "prithibi" }); tem.Add(new person { id = "3", name = "prithibi" });
Now I need to get all the identifiers from the "pr" ListObject, which has no record or an odd number of entries in the "tem" ListObejct. using llamas.
To do this, I used
HashSet<string> inconsistantIDs = new HashSet<string>(pr.Select(p => p.id).Where(p => tem.FindAll(t => t.id == p).Count == 0 || tem.FindAll(t => t.id == p).Count % 2 != 0));
and it works great.
but you can see from the code that I used tem.FindAll (t => t.id == p) .Count twice for comapre with == 0 and % 2! = 0 .
Is it possible to use tem.FindAll (t => t.id == p) .Count once and save it into a temporary variable, and then compare this variable with == 0 and % 2! = 0 .
More simply, I just want to use it once for two conditions here.
list c # lambda linq
Rezoan
source share