我有一个包含" ABC"," DEF"等的值的IEnumerable。
我试图形成一个LINQ查询,其中我的对象中的属性值可能等于我的IEnumerable中包含的许多值之一。我如何形成LINQ查询?
var statesFilter = new List<string>();
statesFilter.Add("NY");
statesFilter.Add("CA");
var employees = new List<Employee>();
employees = getDataFromSomewhere();
// Code below is not working. Just wanted to give you an idea about my LINQ query
var myFilteredList = employees.Where(x => x.State.Contains(statesFilter));
员工类可能是这样的:
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string State { get; set; }
}
答案 0 :(得分:2)
如果State
属性的类型为字符串,则应将Where
更改为以下内容:
var myFilteredList = employees.Where(x => statesFilter.Contains(x.State));
另外,你可以这样做:
var myFilteredList = employees.Where(x => statesFilter.Any(s=>s==x.State));
答案 1 :(得分:1)
条件应该是
{{1}}