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]
)

この場合のexistsは in と同等です。また、ID フィールドがキーでない場合、元の質問に従って、一致する最大 ID を持つすべてのレコードが返されます。

于 2012-07-12T22:43:19.607 に答える