3

我有这个:

vehicles = vehicles.Where(x => x.ModelUrl == filter.ModelUrl);

这工作正常,但我需要传递一个列表,而不是传递单个字符串(作为 filter.ModelUrl),并且查询应该返回其中 x.ModelUrl 是列表中的任何项目的所有结果。

我怎样才能做到这一点?

谢谢

4

6 回答 6

3
vehicles = vehicles.Where(x => OtherList.Contains(x.ModelUrl));

或者

vehicles = vehicles.Where(x => OtherList.Any(ol => ol.ModelUrl == x.ModelUrl));
于 2013-10-18T10:20:05.567 回答
1
vehicles = vehicles.Where(x => list.Contains(x.ModelUrl));
于 2013-10-18T10:19:31.313 回答
0

欢迎

vehicles = vehicles.Where(x => list.Contains(x.ModelUrl));
于 2013-10-18T10:19:41.907 回答
0

Where/方法的替代Contains方法是使用Join

vehicles = vehicles.Join(yourListOfUrls, 
                         vec => vec.ModelUrl,
                         url => url,
                         (vev, url) => vec);

或者

vehicles = from vec in vehicles
           join url in yourListOfUrls on vec.ModelUrl Equals url
           select vec

对于更大的列表,这更快。

于 2013-10-18T10:40:06.390 回答
-1
vehicles = vehicles.Where(x => list.Contains(x.ModelUrl));
于 2013-10-18T10:31:29.487 回答
-1

也许你应该只使用方法Contains

var vehicles = vehicles.Where(x => filter.Contains(x.ModelUrl));
于 2013-10-18T10:27:10.777 回答