1

我有一个包含以下列的表格

inspection_dt,
contact_dt,
description,
product_mod,
product_desc,
contact_nm,
history_id,
inspect_id,
history_type,
incident_product_id,
contact_history_id

我会使用 LINQ 从该表中查询一个通用的行列表。扭曲是我想要 history_id 的最小 (MIN) 值——并模仿这个 SQL 查询。

SELECT DISTINCT
    inspection_dt,
    contact_dt,
    description,
    product_mod,
    product_desc,
    contact_nm,
    MIN(history_id) AS history_id,
    inspect_id,
    history_type,
    incident_product_id,
    contact_history_id
FROM
    myTable
GROUP BY
    inspection_dt,
    contact_dt,
    description,
    product_mod,
    product_desc,
    contact_nm,
    inspect_id,
    history_type,
    incident_product_id,
    contact_history_id

我试过像

var searchData = items
    .GroupBy(i => new { i.history_id })
    .Select(g => new { history = g.Min() })
    .Distinct();

但还是搞砸了

我在使用 MIN、MAX 等函数以及在 LINQ 中进行分组时遇到了困难,我希望能得到任何帮助。

谢谢,

4

2 回答 2

4

如果您想完全模仿查询,则需要按您在 SQL 中分组的相同列或字段进行分组。尝试类似的东西

.GroupBy(item => 
      new 
      {
          item.inspection_dt,
          item.contact_dt,
          item.description,
          item.product_mod,
          item.product_desc,
          item.contact_nm,
          item.inspect_id,
          item.history_type,
          item.incident_product_id
      }
     )
.Select(g => g.Min(item => item.history_id))
于 2011-01-07T17:55:05.097 回答
1

您可以在下面尝试此代码。

我注意到当我在分析器中查看它时它会生成一个 SQL 交叉连接,但我认为它可以满足您的需求,您可能可以对其进行更多调整。

var searchData = items.Select(x => new {x.inspection_dt,x.contact_dt, history= items.Min(j => j.history_id)}).Distinct();

希望这可以帮助

于 2011-01-07T17:52:18.373 回答