例如,我有列表
Rating rate1 = new Rating { artistID = 1, userID = 101, rating = 2 };
Rating rate2 = new Rating { artistID = 1, userID = 102, rating = 4 };
Rating rate3 = new Rating { artistID = 2, userID = 101, rating = 3 };
Rating rate4 = new Rating { artistID = 2, userID = 102, rating = 5 };
Rating rate5 = new Rating { artistID = 2, userID = 103, rating = 1 };
Rating rate6 = new Rating { artistID = 3, userID = 102, rating = 1 };
Rating rate7 = new Rating { artistID = 3, userID = 103, rating = 1 };
List<Rating> ratings = new List<Rating>(7);
ratings.Add(rate1);
ratings.Add(rate2);
ratings.Add(rate3);
ratings.Add(rate4);
ratings.Add(rate5);
ratings.Add(rate6);
ratings.Add(rate7);
我有第二个清单,
List<Rating> ratings2 = new List<Rating>(2);
ratings2.Add(rate1);
ratings2.Add(rate3);
现在我要使用List<Rating> ratings
过滤List<Rating> ratings2
。
在此cace中,我们使用artistID
元素来过滤评分列表,换句话说,我想要包含列出artistID
1 AND 2 的所有列表在评级2
谢谢。
答案 0 :(得分:3)
如果您只想比较artistID
,那么您可以这样做:
List<Rating> resultList = ratings.Where(r => ratings2.Select(t => t.artistID)
.Contains(r.artistID))
.ToList();
以上将选择artistIDs
作为第二个列表ratings2
进行类似于SELECT * from Table WHERE ID IN (1,2)
的比较
答案 1 :(得分:3)
您可以使用HashSet<int>
作为快速查找:
var r2Artists = new HashSet<int>(ratings2.Select(r2 => r2.artistID));
var result = ratings.Where(r => r2Artists.Contains(r.artistID));