将SQL查询转换为LINQ查询不起作用

时间:2013-07-11 20:14:49

标签: c# sql linq

我遇到这种情况:

select max(id) from OTX group by AccNo

我想将其转换为LINQ查询但不起作用。我试过这个但是说Message =“成员'XX'没有支持的SQL翻译。”:

var result = from otx in datacTx.OTX
             group otxCf by otxCf.AccNo
             into Client
             select Client.Max().ID;

2 个答案:

答案 0 :(得分:2)

尝试

var result = from otx in datacTx.OTX
         group otxCf by otxCf.AccNo
         into Client
         select Client.Max(r=>r.id);

或者如果你想和

一样
select AccNo, max(id) from OTX group by AccNo

然后尝试

var result = from otx in datacTx.OTX
         group otxCf by otxCf.AccNo
         into Client
         select new { AccNo = Client.Key , MaxValue= Client.Max(r=>r.id) } ;

答案 1 :(得分:0)

var result = from otx in datacTx.OTX
             group otxCf by otxCf.AccNo
             into Client
             select new { MaxId = Client.Max(s => s.ID)};