[英]Filter in linq with ID's in a List<int>
I need do a filter that request data with a parameter included in a list. 我需要做一个过滤器,使用列表中包含的参数来请求数据。
if (filter.Sc.Count > 0)
socios.Where(s => filter.Sc.Contains(s.ScID));
I try on this way but this not work, I tried also... 我尝试这种方式,但这不起作用,我也试过......
socios.Where( s => filter.Sc.All(f => f == s.ScID));
How I can do a filter like this? 我怎么能这样做过滤器?
socios.Where(s => filter.Sc.Contains(s.ScID));
returns a filtered query. 返回已过滤的查询。 It does not modify the query.
它不会修改查询。 You are ignoring the returned value.
您忽略了返回的值。 You need something like:
你需要这样的东西:
socios = socios.Where(s => filter.Sc.Contains(s.ScID));
but depending on the type of socios
the exact syntax may be different. 但根据类型
socios
确切的语法可以是不同的。
In addition to needing to use the return value of your LINQ .Where()
, you have a potential logic error in your second statement. 除了需要使用LINQ
.Where()
的返回值之外,您的第二个语句中还存在潜在的逻辑错误。 The equivalent logic for a .Contains()
is checking if Any of the elements pass the match criteria. .Contains()
的等效逻辑是检查任何元素是否通过了匹配条件。 In your case, the second statement would be 在你的情况下,第二个声明是
var filteredSocios = socios.Where( s => filter.Sc.Any(f => f == s.ScID));
Of course if you can compare object-to-object directly, the .Contains()
is still adequate as long as you remember to use the return value. 当然,如果你可以直接比较object-to-object,只要你记得使用返回值,
.Contains()
仍然是足够的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.