【问题标题】:LINQ Group By (VB.net & Linq to Entities)LINQ Group By (VB.net & Linq to Entities)
【发布时间】: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 人,但不是所有的行都必须以 _ 结尾?
  • 没有。这是我最不关心的问题。 :-)

标签: vb.net linq


【解决方案1】:

在将大量直接 SQL 查询转换为 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

会是这样的(只是在这里输入而不检查,但这应该很接近):

(FROM instrumentEntity in dbContext.InstrumentEntities
GROUP Join documentInstrumentEntity
    ON instrumentEntity.InstrumentId equals documentInstrumentEntity.InstrumentId Into foundDocumentInstruments = Group
    FROM foundDocumentInstrument in foundDocumentInstruments.DefaultIfEmpty()
SELECT NEW With 
{
.Code = instrumentEntity.Code,
.Name = instrumentEntity.Name
}).GroupBy(Function(result) New With 
    {
    Key result.Code,
    Key result.Name
    }).ToList()

使用 VB.Net 时,如果您按多个属性分组,则在每个字段上使用“键”标识符非常重要,否则分组将不起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 2010-12-31
    • 2023-04-08
    • 1970-01-01
    • 2019-08-09
    相关资源
    最近更新 更多