C# 按嵌套列表的属性值筛选对象列表等于嵌套列表的另一个元素的相同属性值
好的,我有一节课:C# 按嵌套列表的属性值筛选对象列表等于嵌套列表的另一个元素的相同属性值,c#,sql,linq,linq-to-sql,C#,Sql,Linq,Linq To Sql,好的,我有一节课: public class Person { public int Id { get; set; } public string SpecialNumber { get; set; } public IQueryable<Game> Games { get; set; } } 我需要显示人的特殊编号,其游戏总数RamNeeded最大,来自拥有游戏的人(IQueryable): 来自一个州的两个以上不同城镇 来自不同的州在一个国家(这意味
public class Person
{
public int Id { get; set; }
public string SpecialNumber { get; set; }
public IQueryable<Game> Games { get; set; }
}
我需要显示人的特殊编号
,其游戏总数RamNeeded
最大,来自拥有游戏的人(IQueryable
):
来自一个州的两个以上不同城镇
来自不同的州在一个国家(这意味着至少有两场不同州的比赛)
来自不同的国家(这意味着与不同国家至少有两场比赛)
我需要在LINQ或SQL上进行此查询。希望您能提供帮助。只要您需要对具有某个属性的相等值的集合的元素执行某些操作,您就可以使用方法(或查询语法中的子句)
然后可以对每组元素使用不同的聚合函数。例如,在您的案例中,可用于检查组是否包含特定数量的项目
话虽如此,问题可能是这样的
IQueryable<Person> persons = ...;
var query =
from person in persons
let countryGroups = person.Games.GroupBy(game => game.Town.CountryName)
where countryGroups.Count() > 1 // (3)
&& countryGroups.Any(countryGroup =>
countryGroup.GroupBy(game => game.Town.StateName).Count() > 1 // (2)
&& countryGroup.GroupBy(game => game.Town.StateName).Any(stateGroup =>
stateGroup.GroupBy(game => game.Town.Id).Count() > 2)) // (1)
let RamNeeded = person.Games.Sum(game => game.RamNeeded) // in case you need to include it in the select
orderby RamNeeded descending
select person.SpecialNumber;
var result = query.FirstOrDefault();
IQueryable人员=。。。;
变量查询=
亲自
让countryGroups=person.Games.GroupBy(game=>game.Town.CountryName)
其中countryGroups.Count()>1/(3)
&&countryGroups.Any(countryGroup=>
countryGroup.GroupBy(game=>game.Town.StateName.Count()>1/(2)
&&countryGroup.GroupBy(game=>game.Town.StateName).Any(stateGroup=>
stateGroup.GroupBy(game=>game.Town.Id.Count()>2))/(1)
让RamNeeded=person.Games.Sum(game=>game.RamNeeded)//以防您需要将其包含在select
orderby需要降序
选择person.SpecialNumber;
var result=query.FirstOrDefault();
什么部分给您带来了困难?(1)、(2)、(3)过滤条件是否正确?它们是如何组合的-和
或或
?@DanBracuk如何在第一时间比较LINQ中城镇名称的值place@IvanStoev是的,它们组合为和
IQueryable<Person> persons = ...;
var query =
from person in persons
let countryGroups = person.Games.GroupBy(game => game.Town.CountryName)
where countryGroups.Count() > 1 // (3)
&& countryGroups.Any(countryGroup =>
countryGroup.GroupBy(game => game.Town.StateName).Count() > 1 // (2)
&& countryGroup.GroupBy(game => game.Town.StateName).Any(stateGroup =>
stateGroup.GroupBy(game => game.Town.Id).Count() > 2)) // (1)
let RamNeeded = person.Games.Sum(game => game.RamNeeded) // in case you need to include it in the select
orderby RamNeeded descending
select person.SpecialNumber;
var result = query.FirstOrDefault();