我有一个带有以下列的桌子

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

我将使用LINQ查询此表中的行的通用列表。扭曲是我想要历史记录的最小(最小)值 - 并模仿此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中进行分组会卡住,并感谢我能获得的任何帮助。

谢谢,

有帮助吗?

解决方案

如果要完全模仿查询,则需要按照您在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))

其他提示

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

我注意到当我在Profiler中查看它时,它会生成一个SQL交叉加入,但是我认为它可以完成您想要的功能,并且您可能可以对其进行更多调整。

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

希望这可以帮助

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top