大家好,我有一个使用Linq的查询,返回一组记录,其中之一是国家ID,我还有一个期望的国家数组。是否有一种方法可以循环遍历国家数组,并查看结果中是否存在该ID?我想做类似以下的操作:
results = from r in results
where
//jump to my c# array
for(int x = 0;x < array.count; x++)
{
r.countryId.ToString().Contains(array[x])
}
select r
谢谢
var list = from r in results
where array.Contains(r.countryId.ToString())
select r;
var query = from a in results
join b in array
on a.CountryId equals b.CountryId
select a;
您没有给出数组类型,因此假设它是testclass1
class TestClass1
{
public int Id { get; set; }
public string Name { get; set; }
}
class Country
{
public int Id { get; set; }
public string Name { get; set; }
}
List<Country> countries = new List<Country>();
TestClass1[] arrays = new TestClass1[30];
countries.Where(x => arrays.Select(y => y.Id).Contains(x.Id)).ToList();
我不确定这是否是最佳方法,但我认为这将起作用。
更新:我没有注意到数组也是国家类型。抱歉。