[英]How to find all items in a list where a property has a specific value or where there is a related item with this value?
How to get(or filter) list where there are record with the same userID and artistID 如何获取(或过滤)具有相同userID和artistID的记录的列表
Here is my rating object 这是我的评价对象
public class Rating
{
public int userID { get; set; }
public int artistID { get; set; }
public int rating { get; set; }
}
and here is my data 这是我的数据
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 = 3, userID = 102, rating = 1 };
List<Rating> ratings = new List<Rating>(2);
ratings.Add(rate1);
ratings.Add(rate2);
ratings.Add(rate3);
ratings.Add(rate4);
ratings.Add(rate5);
Output if there ara record : where userID = 101, and where (artistID where userID is 101) 如果存在ara记录,则输出:userID = 101,以及where(artistID,其中userID为101)
Output example that i want to have: 我想要的输出示例:
artistID userID rating
1 101 2
1 102 4
2 101 3
2 102 5
I want also 1, 102, 4
because there is another rating with this artistID
which userID is 101. The same applies to 2, 102, 5
. 我想也1, 102, 4
,因为有另一个等级与此artistID
其中userID是101.这同样适用于2, 102, 5
。
Update If You want to include all records where there is another record with a different user but the same artist (quote from @Tim Schmelter). 更新 ,如果你想包括所有记录,其中有一个不同的用户,但同一艺术家 (从@Tim Schmelter报价) 再创历史新高 。 You can found answer from @Tim Schmelter in Update version. 您可以在更新版本的@Tim Schmelter中找到答案。
For example, if u change rate5 to Rating rate5 = new Rating { artistID = 3, userID = 101, rating = 1 };
例如,如果您将rate5更改为Rating rate5 = new Rating { artistID = 3, userID = 101, rating = 1 };
and also add new object rate6 Rating rate6 = new Rating { artistID = 3, userID = 102, rating = 1 };
并添加新的对象rate6 Rating rate6 = new Rating { artistID = 3, userID = 102, rating = 1 };
It will make result: 它将产生结果:
artistID userID rating
1 101 2
1 102 4
2 101 3
2 102 5
3 101 1
3 102 1
Because artistID that rated by userID-101 also rated by userid-102 you can found answer in @Tim Schmelter answer. 因为由userID-101评分的artistID也由userid-102评分,所以您可以在@Tim Schmelter答案中找到答案。
So you want all ratings where either the userID
is 101 or there is one with the same artistID
that has userID
= 101? 因此,您是否需要所有评级,其中userID
为101或具有相同artistID
userID
= 101的artistID
? You can use Any
in Where
: 您可以Where
以下位置使用Any
:
var query = ratings.Where(r => r.userID == 101 ||
ratings.Any(rr => rr.userID == 101 && r.artistID == rr.artistID));
This is similar to a correlated subquery in sql. 这类似于sql中的相关子查询。
Test: 测试:
foreach (Rating r in query)
Console.WriteLine("artistID = {0}, userID = {1}, rating = {2}"
, r.artistID, r.userID, r.rating);
Result: 结果:
artistID = 1, userID = 101, rating = 2
artistID = 1, userID = 102, rating = 4
artistID = 2, userID = 101, rating = 3
artistID = 2, userID = 102, rating = 5
Update "i want all records where there is another record with a different user but the same artist" 更新 “我想要所有记录,其中有另一条记录具有不同的用户但同一位艺术家”
Now it's clear, you want this: 现在很清楚,您需要这样做:
var query = ratings
.Where(r => ratings
.Any(rr => r.artistID == rr.artistID && rr.userID != r.userID));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.