简体   繁体   English

如何在列表中查找属性具有特定值或具有与此值的相关项的所有项?

[英]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.

相关问题 是否有Linq操作从项目列表中检索特定项目,其中该项目具有应为唯一的属性的属性值? - Is there a Linq operation to retrieve specific items from a list of items where the item has a property value for a property which should be unique? 查找日期属性在特定时间间隔内的所有列表项 - Find all list items where a date property is in a specific interval 仅Concat列表中的属性具有特定值 - Only Concat List where a property has a specific value 使用 LINQ 通常查找项目属性值相等的任何项目 - Generically find any items where item property value is equal using LINQ C# 从列表中取出 x 项,其中每个项都有一个特定属性的唯一值 - C# take x items from list where each one has a unique value for a certain property 从对象列表中删除属性“ colName”的值与给定数组“ AllCols”中的任何项匹配的所有对象 - From a list of objects remove all objects where value of a property “colName” matches Any item in a given array “AllCols” 如何从具有相同属性值的列表中删除项(计数大于2) - How to remove Items from a list with the same property value, where the count is greater than 2 选择模型属性在键值对列表中包含全部的位置 - Select where model property contains all in list of key value pairs Linq 选择实体属性值与另一个列表中任何项目的属性值匹配的位置 - Linq select where entity property value matches value of property of any item in another List 如何从C#列表中查找具有特定值的随机项目? - How to find a random item from a C# list that has a specific value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM