【发布时间】:2012-10-11 14:01:15
【问题描述】:
我有一份客户名单:
List<customer> customerList;
我只想获得 Country="India" 和 Status="A" 的客户。
我试过这个:
List<customer> customerList=customerList.Where(p=>p.Country.Equals("India") && p.Status.Equals("A")).ToList();
和
List<customer> customerList=customerList.Where(p=>p.Country.Equals("India")).Where(p=>p.Status.Equals("A")).ToList();
但都没有返回任何东西。
如果我像下面的例子那样划分条件,那么记录被正确获取。
List<customer> customerList=customerList.Where(p=>p.Country.Equals("India")).ToList();
customerList=customerList.Where(p=>p.Status.Equals("A")).ToList();
我想知道如何在单个查询中使用 AND 条件过滤对象。
谁能告诉,有什么好办法,而不是调用 where 条件。
【问题讨论】:
-
您确定至少有一个项目同时满足这两个条件吗?您的前 2 个查询对我来说似乎是正确的......
-
显示您的测试数据,因为这 2 个 linq 语句看起来是正确的。
-
Country和Status是字符串变量,还是其他类型? -
您能否考虑将答案标记为正确甚至完全回复?