C# 按计数对象分组
我想计算我分组的项目数。我有一个类型为Liquidity Builder的列表:C# 按计数对象分组,c#,linq,C#,Linq,我想计算我分组的项目数。我有一个类型为Liquidity Builder的列表: public class LiquidityBuilder { private string _lp; private string _timezone; private int _size; private bool _ask; public string LP { get { return _lp; } } public int Size { get { return _size; } }
public class LiquidityBuilder
{
private string _lp;
private string _timezone;
private int _size;
private bool _ask;
public string LP
{
get { return _lp; }
}
public int Size
{
get { return _size; }
}
public string Timezone
{
get { return _timezone; }
}
public bool Ask
{
get { return _ask; }
}
public LiquidityBuilder(string lp, string timezone, int size, bool ask)
{
_lp = lp;
_timezone = timezone;
_size = size;
_ask = ask;
}
}
数据如下所示:
LP Timezone Size Ask
GS LDN 1000000 True
GS LDN 3000000 True
GS TOR 2000000 True
JPM TOR 1000000 False
CS ASIA 1000000 True
JPM TOR 1000000 False
CITI TOR 2000000 False
我需要使用键LP、时区和大小来计算组数,例如
GS LDN真-->计数:2
以下是我所拥有的:
var result = liquidtyTimezoneData.GroupBy(x => new { x.LP, x.Timezone, x.Ask })
.Select(x => new
{
LP = x.Key.LP,
Timezone = x.Key.Timezone,
Ask = x.Key.Ask,
Sum = x.Sum(z => z.Size)
});
有什么建议吗?只需使用x.Count()
获取组中项目的计数。示例
var result = liquidtyTimezoneData.GroupBy(x => new { x.LP, x.Timezone, x.Ask });
foreach (var r in result)
{
Console.WriteLine("LP: {0} TZ: {1} Ask: {2} Count: {3}", r.Key.LP, r.Key.Timezone, r.Key.Ask, r.Count());
}
x、 Key.Count()应该可以实现我想的技巧欢迎来到StackOverflow!请看,如果共识是“不,他们不应该”,请尝试为您的问题找到一个更有意义的标题!