我使用LINQ内连接组合了2个表客户端和付款。在此之后,我用这两个表中的值填写一个新模型Totals。我只需要Username
。有任何想法吗?我会非常感激的!
var results = from client in _db.Clients
join trade in _db.Payments on client.Id equals trade.ClientId
where (client.Id == trade.ClientId &&
DateTime.Compare(trade.TradeDate, (DateTime)fromDate) >= 0 &&
DateTime.Compare(trade.TradeDate, (DateTime) toDate) <= 0)
select new { client.Username, trade};
var cs = results
.GroupBy(tr => tr.trade.ClientId)
.Select(item => new Totals
{
Username = //here I need to set a value
CommBcf = item.Sum(client => client.trade.CommBcf),
EcnAll = item.Sum(client => client.trade.EcnAll),
EcnRbt = item.Sum(client => client.trade.EcnRbt),
OpenGross = item.Sum(client => client.trade.OpenGross),
CloseGross = item.Sum(client => client.trade.CloseGross)
}).ToList();
答案 0 :(得分:3)
按键和名称分组。您可以通过使用new {id,name}
创建新的匿名对象来按多列分组。
var results = from client in _db.Clients
join trade in _db.Payments on client.Id equals trade.ClientId
where (client.Id == trade.ClientId &&
DateTime.Compare(trade.TradeDate, (DateTime)fromDate) >= 0 &&
DateTime.Compare(trade.TradeDate, (DateTime) toDate) <= 0)
select new { client.Username, trade};
var cs = results
.GroupBy(tr => new {id=tr.trade.ClientId,name=tr.trade.Name})
.Select(item => new Totals
{
Username = item.Key.name
CommBcf = item.Sum(client => client.trade.CommBcf),
EcnAll = item.Sum(client => client.trade.EcnAll),
EcnRbt = item.Sum(client => client.trade.EcnRbt),
OpenGross = item.Sum(client => client.trade.OpenGross),
CloseGross = item.Sum(client => client.trade.CloseGross)
}).ToList();