0

如何将其转换为 LinQ?

SELECT *
FROM YourTable
WHERE id IN (
    SELECT MAX(id) FROM YourTable GROUP BY alarmId, alarmUnit
    )

到目前为止尝试过但不起作用

 var a= from x in YourTable .ToList()
                                  group x by new { x.alarmId, x.alarmUnit} into g
                                  from x1 in YourTable 
                                  where x1.ID == (from x2 in g select x2.ID).Max()
                                  select x1;

谢谢,

4

2 回答 2

0

试试这个:

var a= from x in YourTable .ToList()
       group x by new { x.alarmId, x.alarmUnit} into g
             select new
             {
               alarmId = g.Key.alarmId,
               alarmUnit = g.Key.alarmUnit,
               id = g.Max(b => b.id)
             };
于 2012-07-12T21:39:40.557 回答
0
var a = from x in YourTable
        where (
            from y in YourTable
            group y by new { y.alarmId, y.alarmUnit } into g
            select g.Max(z => z.ID)
        ).Contains(x.ID)
        select x;

这应该产生以下 SQL

SELECT [t0].[ID] AS [ID], [t0].[alarmId] AS [alarmId], ...
FROM [YourTable] AS [t0]
WHERE EXISTS(
    SELECT NULL AS [EMPTY]
    FROM (
        SELECT MAX([t1].[ID]) AS [value]
        FROM [YourTable] AS [t1]
        GROUP BY [t1].[alarmId], [t1].[alarmUnit]
    ) AS [t2]
    WHERE [t2].[value] = [t0].[ID]
)

在这种情况下存在等同于in。此外,如果 ID 字段不是键,它将根据您的原始问题返回具有匹配最大 ID 的所有记录。

于 2012-07-12T22:43:19.607 回答