我有 2 个列表对象类型的某个类,
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" });
现在我必须从“tem” ListObejct中没有条目或奇数条目的“pr” ListObject 中获取所有 id 。使用拉姆达。
为此,我使用过,
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));
它工作正常。
但是您可以从我使用的代码中看到tem.FindAll(t => t.id == p).Count两次与==0和%2!=0比较。
有什么方法可以使用tem.FindAll(t => t.id == p).Count一次并将其保存到一个临时变量中,然后将此变量与 ==0和%2!=0进行比较。
更简单地说,我只想在这里为两个条件使用一次。