我有以下linq查询:
(From document In DocumentEntities
Group Join transactionRow In TransactionEntities.Where(Function(tran) tran.PaymentType = "Charge" And tran.Amount <> 0 And (Not tran.Void.HasValue OrElse (tran.Void.HasValue AndAlso Not tran.Void.Value)))
On document.DocumentId Equals transactionRow.DocumentId
Into foundTransactions = Group
From foundTransaction In foundTransactions.DefaultIfEmpty()
Group Join documentInstrument In DocumentInstrumentEntities.Where(Function(docInst) docInst.IsPrimary)
On document.DocumentId Equals documentInstrument.DocumentId
Into relatedDocumentInstruments = Group
From documentInstrument In relatedDocumentInstruments.DefaultIfEmpty()
Join instrument In InstrumentEntities
On documentInstrument.InstrumentId Equals instrument.InstrumentId
Where (document.DocumentId = 241083 OR document.DocumentId = 241084 OR document.DocumentId = 241085 OR document.DocumentId = 241086 OR document.DocumentId = 241087 OR document.DocumentId = 241090 OR document.DocumentId = 241091 OR document.DocumentId = 241092 OR document.DocumentId = 241093 OR document.DocumentId = 241094 OR document.DocumentId = 241095 OR document.DocumentId = 241096 OR document.DocumentId = 241097 OR document.DocumentId = 241098 OR document.DocumentId = 241099 OR document.DocumentId = 241100 OR document.DocumentId = 241101 OR document.DocumentId = 241088 OR document.DocumentId = 241089)
Order By instrument.Name
Select New With
{
.Amount = foundTransaction.Amount,
.Code = instrument.Code,
.Instrument = instrument.Name,
.InstrumentId = instrument.InstrumentId,
.Pages = document.Pages
}).GroupBy(Function(instrument) new with {instrument.Code, instrument.Instrument})
.Select(Function(grouping) new with {.Code = grouping.Key.Code, .Instrument = grouping.Key.Instrument})
这给了我一个很好的分组的仪器和代码列表,按instrument.name排序。现在我需要知道如何访问实际分组中的项目来执行聚合功能。例如,我想在最终结果中返回SUM(金额),但我不知道如何访问每个键的实际IGrouping,以便我可以获得所需的聚合。
所以,例如,我希望我的新FINAL选择看起来像这样:
.Select(Function(grouping) new with {.Code = grouping.Key.Code, .Instrument = grouping.Key.Instrument, .Amount = SUM(grouping.amount)})
我知道在聚合函数中会有一个lambda,但我只是伪代码,所以我可以试着解释一下我的追求。
感谢您的帮助!
答案 0 :(得分:1)
一个组是一个序列。 所以,试试吧:
.Amount = grouping.Sum(Function(item) item.Amount)
答案 1 :(得分:0)
发布的建议是正确的语法,我尝试了但是得到了异常,所以我需要在groupby之后调用.ToList()。
以下是有效的查询:
(From document In DocumentEntities
Group Join transactionRow In TransactionEntities.Where(Function(tran) tran.PaymentType = "Charge" And tran.Amount <> 0 And (Not tran.Void.HasValue OrElse (tran.Void.HasValue AndAlso Not tran.Void.Value)))
On document.DocumentId Equals transactionRow.DocumentId
Into foundTransactions = Group
From foundTransaction In foundTransactions.DefaultIfEmpty()
Group Join documentInstrument In DocumentInstrumentEntities.Where(Function(docInst) docInst.IsPrimary)
On document.DocumentId Equals documentInstrument.DocumentId
Into relatedDocumentInstruments = Group
From documentInstrument In relatedDocumentInstruments.DefaultIfEmpty()
Join instrument In InstrumentEntities
On documentInstrument.InstrumentId Equals instrument.InstrumentId
Where (document.DocumentId = 241083 OR document.DocumentId = 241084 OR document.DocumentId = 241085 OR document.DocumentId = 241086 OR document.DocumentId = 241087 OR document.DocumentId = 241090 OR document.DocumentId = 241091 OR document.DocumentId = 241092 OR document.DocumentId = 241093 OR document.DocumentId = 241094 OR document.DocumentId = 241095 OR document.DocumentId = 241096 OR document.DocumentId = 241097 OR document.DocumentId = 241098 OR document.DocumentId = 241099 OR document.DocumentId = 241100 OR document.DocumentId = 241101 OR document.DocumentId = 241088 OR document.DocumentId = 241089)
Order By instrument.Name
Select New With
{
.Amount = foundTransaction.Amount,
.Code = instrument.Code,
.Instrument = instrument.Name,
.InstrumentId = instrument.InstrumentId,
.Pages = document.Pages
}).GroupBy(Function(instrument) new with {instrument.Code, instrument.Instrument}).ToList().Select(Function(grouping) new with {.Code = grouping.Key.Code, .Instrument = grouping.Key.Instrument, .TotalFees= grouping.Sum(Function(item) item.Amount)}).OrderBy(Function(item) item.Instrument).ThenBy(Function(item) item.Code).ToList()