C# LINQ表达式,用于按字符串属性选择对象,其队列属性中对象的最大计数不重复
我有一个记录对象队列,如下所示:C# LINQ表达式,用于按字符串属性选择对象,其队列属性中对象的最大计数不重复,c#,linq,C#,Linq,我有一个记录对象队列,如下所示: public class Record { public string TypeDesc { get; set; } public Queue<Total> Totals { get; set; } etc..... } 公共类记录 { 公共字符串TypeDesc{get;set;} 公共队列总数{get;set;} 等 } 我在编写LINQ表达式来提取一个子集时遇到了问题,该子集中
public class Record
{
public string TypeDesc { get; set; }
public Queue<Total> Totals { get; set; }
etc.....
}
公共类记录
{
公共字符串TypeDesc{get;set;}
公共队列总数{get;set;}
等
}
我在编写LINQ表达式来提取一个子集时遇到了问题,该子集中只有每个TypeDesc中的一个,但在每个TypeDesc中,有一个在Totals队列中包含最多对象
我不确定这是否重要,但只有一个TypeDesc在Totals队列属性中包含Total对象。队列中的所有其他人都是空的。大约有8个唯一的TypeDesc值
这是我的尝试,但totals属性在“s”上不可用
var records=records.Select(c=>c.TypeDesc).Where(s=>s.Totals.Count).Max().Distinct()
TypeDesc
属性对记录进行分组records.GroupBy(r => r.TypeDesc)
.Select(
g => g.Aggregate((acc, current) => current.Totals.Count > acc.Totals.Count
? current
: acc));
对于这样的复杂查询,最好将逻辑分解一点,以使代码更具可读性:
Func<IEnumerable<Record>, Record> mostTotals =
group => group.Aggregate(
(acc, current) => current.Totals.Count > acc.Totals.Count
? current
: acc);
var records = records.GroupBy(r => r.TypeDesc)
.Select(mostTotals);
TypeDesc
属性对记录进行分组records.GroupBy(r => r.TypeDesc)
.Select(
g => g.Aggregate((acc, current) => current.Totals.Count > acc.Totals.Count
? current
: acc));
对于这样的复杂查询,最好将逻辑分解一点,以使代码更具可读性:
Func<IEnumerable<Record>, Record> mostTotals =
group => group.Aggregate(
(acc, current) => current.Totals.Count > acc.Totals.Count
? current
: acc);
var records = records.GroupBy(r => r.TypeDesc)
.Select(mostTotals);
TypeDesc
属性对记录进行分组records.GroupBy(r => r.TypeDesc)
.Select(
g => g.Aggregate((acc, current) => current.Totals.Count > acc.Totals.Count
? current
: acc));
对于这样的复杂查询,最好将逻辑分解一点,以使代码更具可读性:
Func<IEnumerable<Record>, Record> mostTotals =
group => group.Aggregate(
(acc, current) => current.Totals.Count > acc.Totals.Count
? current
: acc);
var records = records.GroupBy(r => r.TypeDesc)
.Select(mostTotals);
TypeDesc
属性对记录进行分组records.GroupBy(r => r.TypeDesc)
.Select(
g => g.Aggregate((acc, current) => current.Totals.Count > acc.Totals.Count
? current
: acc));
对于这样的复杂查询,最好将逻辑分解一点,以使代码更具可读性:
Func<IEnumerable<Record>, Record> mostTotals =
group => group.Aggregate(
(acc, current) => current.Totals.Count > acc.Totals.Count
? current
: acc);
var records = records.GroupBy(r => r.TypeDesc)
.Select(mostTotals);