【发布时间】:2013-09-10 13:19:58
【问题描述】:
我有 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" });
现在我必须从 "pr" ListObject 中获取 no entry 或 odd number of entries 在 中的所有 id >“tem” ListObejct。使用 lamda。
为此我使用过,
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。
更简单地说,我只想在这里为两个条件使用一次。
【问题讨论】:
-
请不要将主题用作“xxx中面临的问题”,这将是google-link。