【发布时间】:2015-06-09 02:13:54
【问题描述】:
我正在尝试让一个小组在 Linq 中工作:
from document in Documents
Group Join documentInstrument In DocumentInstruments
On document.DocumentId Equals documentInstrument.DocumentId
Into relatedDocumentInstruments = Group
From documentInstrument In relatedDocumentInstruments.DefaultIfEmpty()
Group Join instrument In Instruments
On documentInstrument.InstrumentId Equals instrument.InstrumentId
Into relatedInstruments = Group
From instrument In relatedInstruments.DefaultIfEmpty()
where (document.DocumentId = 1294201 Or document.DocumentId = 1294179)
Order by instrument.Name
Select New With
{
.Code = instrument.Code,
.InstrumentName = instrument.Name
}
这基本上给我留下了重复(相同的代码和名称)。我已经尝试了几乎所有使用 google 可以找到的“Group By”语法,但似乎没有任何效果。
这是我试图转换为 Linq(到实体)的原始查询:
Sql = "SELECT Instruments.Name AS Instrument, Instruments.Code, COUNT(Instruments.Name) AS InstrumentCount, " & _
"SUM(Documents.Pages) AS TotalPages, SUM(Fees.FeeAmount) AS FeeAmount " & _
"FROM Documents LEFT JOIN DocInstruments ON Documents.DocID = DocInstruments.DocID AND DocInstruments.IsPrimary = <TRUE /> " & _
"LEFT JOIN Instruments ON DocInstruments.InstrumentID = Instruments.InstrumentID " & _
"LEFT JOIN (SELECT DocumentID, SUM(Amount) AS FeeAmount FROM Transactions WHERE PaymentType = 'Charge' and Void = <FALSE /> " & _
"GROUP BY DocumentID) AS Fees ON Documents.DocID = Fees.DocumentID " & _
"WHERE Documents.DocID IN (" & sList & ") " & sInstWhere & _
"GROUP BY Instruments.Name, Instruments.Code " & _
"ORDER BY Instrument"
** 我暂时将 Transactions 表的连接从我的 linq 查询中移除,只是尝试在添加更多之前通过代码/名称使组工作**
编辑:
好的,如果我能弄清楚如何将以下查询放入 linq,那将是一个好的开始。我只是似乎无法通过...加入该组
SELECT i.Code, i.Name
from Instruments i
LEFT JOIN DocInstruments di on i.InstrumentId = di.InstrumentId
group by i.Code, i.Name
order by i.Code
似乎它不应该那么难,一旦你知道语法,它可能不会,但我无法让任何东西工作,Linqer 不会连接到我的 EF 版本。无赖。
有任何 linq 专家可以提供帮助吗?
【问题讨论】:
-
我不是 vb 人,但不是所有的行都必须以 _ 结尾?
-
没有。这是我最不关心的问题。 :-)