假如利用select KeyValue,ID,Status from ComputingResult 得到一个实体Temp,实体中三个字段KeyValue1,ID1,Status1 分别对应select语句中的三个字段,接下来如何使用Linq操作Temp,得到以下sql语句的效果:
select KeyValue,ID,Status from ComputingResult where ID in ( select max(ID) as ID from ComputingResult where RegularRecord = 1 and LLKI='a' group by KeyValue)写到一半不会写了:
var temp1 = from p in Temp
                        where p.ID1 == LCRNormal.Max(p2 => p2.ID1)
                        group p by p.KeyValue;

解决方案 »

  1.   


    select KeyValue,ID,Status from ComputingResult where ID in ( select max(ID) as ID from ComputingResult where RegularRecord = 1 and LLKI='a' group by KeyValue)
    =========
    var query=from cr in ComputingResult
              let listId=from crt in ComputingResult
                         where crt. RegularRecord == 1 && crt.LLKI=="a"
                         group crt by crt.Keyvalue into m
                         select m.Max(n=>n.ID)
              where listId.Contains(cr.ID)
              select new 
              {
                 crt.Keyvalue,
                 crt.ID,
                 crt.Status
              };
      

  2.   

    select KeyValue,ID,Status from ComputingResult where ID in ( select max(ID) as ID from ComputingResult where RegularRecord = 1 and LLKI='a' group by KeyValue)
    =========
    var query=from cr in ComputingResult
              let listId=from crt in ComputingResult
                         where crt. RegularRecord == 1 && crt.LLKI=="a"
                         group crt by crt.Keyvalue into m
                         select m.Max(n=>n.ID)
              where listId.Contains(cr.ID)
              select new   //代码手写  此处更正一下
              {
                 cr.Keyvalue,
                 cr.ID,
                 cr.Status
              };
      

  3.   

    linq这块比较迷糊,请问上述代码效果是不是先做group crt by crt.Keyvalue 并返回最大值,在group  by、max聚合后的数据基础上,再做where语句后的条件筛选呢?
      

  4.   

    先获取listId 得到 in的条件 
    再执行select KeyValue,ID,Status from ComputingResult where ID in ( )语句
      

  5.   

    http://download.csdn.net/source/2475342你可以利用这个插件在VS中查看上述语句翻译成的SQL语句 应该就很容易理解了
      

  6.   

    var temp1 = from p in ComputingResult
                let maxIDs = from p1 in ComputingResult 
                             where p1.RegularRecord == 1 && p1.LIKI == "a"
                             group p1 by p1.KeyValue into g
                             select g.Max(x => x.ID)
                where maxIDs.Contains(p.ID)
                select p;
                             
      

  7.   

      先where 条件,,然后在排序
      

  8.   

    看你的代码是 先分组,取每组id最大的一条, 可以先分组,再id降序,去第一条var query = Temp.Where(w => w.RegularRecord == 1 && w.LLKI == "a")
                    .GroupBy(g => g.KeyValue)
                    .Select(s => s.OrderByDescending(o => o.ID).FirstOrDefault())
                    .Select(s => new { KeyValue = s.KeyValue, ID = s.ID, Status = s.Status });
     //var query = Temp.Where(w => w.RegularRecord == 1 && w.LLKI == "a")
                 //    .GroupBy(g => g.KeyValue)
                 //    .Select(s => s.OrderByDescending(o => o.ID).FirstOrDefault());
      

  9.   

    加菲猫,我的疑问是这样,不知以下描述的筛选顺序是否正确?
    var query=from cr in ComputingResult
              let listId=from crt in ComputingResult
                         where crt. RegularRecord == 1 && crt.LLKI=="a"    //在第一批筛选的基础数据上第二批筛选
                         group crt by crt.Keyvalue into m    //第一批筛选
                         select m.Max(n=>n.ID)       //第一批筛选
              where listId.Contains(cr.ID)   
              select new   //代码手写  此处更正一下
              {
                 cr.Keyvalue,
                 cr.ID,
                 cr.Status
              };
      

  10.   

    linq会 但是lambada老用不好 我日
      

  11.   

    linq to sql  上述代码最终执行时,发送给数据库的只是一段SQL语句  类似select KeyValue,ID,Status from ComputingResult where ID in ( select max(ID) as ID from ComputingResult where RegularRecord = 1 and LLKI='a' group by KeyValue)
    这样的SQL语句  ,数据查询筛选都是在SQL端执行的
    怎么筛选的  这就要看SQL语句在SQL客户端上的执行顺序了
      

  12.   

    我没说清,我的意思是,先执行
      group crt by crt.Keyvalue into m //第一批筛选
      select m.Max(n=>n.ID) //第一批筛选
    然后,在筛选出的数据上再做
      crt. RegularRecord == 1 && crt.LLKI=="a" //在第一批筛选的基础数据上第二批筛选现在是先crt. RegularRecord == 1 && crt.LLKI=="a",然后再group crt by 了,得到的数据是错的。
      

  13.   

    lz 你确认你现在的需求是下面的SQL?select KeyValue,ID,Status from ComputingResult where ID in ( select max(ID) as ID from ComputingResult where RegularRecord = 1 and LLKI='a' group by KeyValue)那不成了select KeyValue,ID,Status from ComputingResult where ID in ( select max(ID) as ID from ComputingResult group by KeyValue) and RegularRecord = 1 and LLKI='a' 
      

  14.   

    是sql写错了,sql是在发帖子时写的>_<!
    应该是:select KeyValue,ID,Status from ComputingResult where ID in ( select max(ID) as ID from ComputingResult group by KeyValue) and RegularRecord = 1 and LLKI='a'
      

  15.   

    那就把 group by 的 where 条件挪到外面就可以了。var query=from cr in ComputingResult
              let listId=from crt in ComputingResult
                         where crt. RegularRecord == 1 && crt.LLKI=="a"                     group crt by crt.Keyvalue into m
                         select m.Max(n=>n.ID)
              where listId.Contains(cr.ID) &&
                    cr.RegularRecord == 1 && 
                    cr.LLKI == "a"
              select cr;
      

  16.   

    var query = Temp.GroupBy(g => g.KeyValue)         
                    .Select(s => s.OrderByDescending(o => o.ID).FirstOrDefault())
                    .Where(w => w.RegularRecord == 1 && w.LLKI == "a")
                    .Select(s => new { KeyValue = s.KeyValue, ID = s.ID, Status = s.Status });这样也行吧,麻烦看下对不?
      

  17.   

    你的 where 和select 应该要换一下var query = Temp.GroupBy(g => g.KeyValue) 
                    .Where(w => w.RegularRecord == 1 && w.LLKI == "a")
                    .Select(s => s.OrderByDescending(o => o.ID).FirstOrDefault())
                    .Select(s => new { KeyValue = s.KeyValue, ID = s.ID, Status = s.Status });
      

  18.   

    不太对,where 放在select里面 var query = Temp.GroupBy(g => g.KeyValue) 
                    .Select(s => s.Where(w=>w.RegularRecord == 1 && w.LLKI == "a")
                                 .OrderByDescending(o => o.ID)
                                 .FirstOrDefault())
                    .Select(s => new { KeyValue = s.KeyValue, ID = s.ID, Status = s.Status });
      

  19.   

    var query = Temp.GroupBy(g => g.KeyValue)   
      .Select(s => s.OrderByDescending(o => o.ID).FirstOrDefault())
      .Where(w => w.RegularRecord == 1 && w.LLKI == "a")
      .Select(s => new { KeyValue = s.KeyValue, ID = s.ID, Status = s.Status });这种测试结果是对的
    var query = Temp.GroupBy(g => g.KeyValue) 
                    .Select(s => s.Where(w=>w.RegularRecord == 1 && w.LLKI == "a")
                                 .OrderByDescending(o => o.ID)
                                 .FirstOrDefault())
                    .Select(s => new { KeyValue = s.KeyValue, ID = s.ID, Status = s.Status });这种还是先w=>w.RegularRecord == 1 && w.LLKI == "a",然后再OrderByDescending、FirstOrDefault了感谢!