我在构建Linq查询时做错了什么

时间:2012-06-25 15:08:22

标签: c# linq

我正在尝试为后续查询编写等效的linq代码。

SELECT A.*  
FROM  
(  
    SELECT * FROM TableA   
    WHERE id = 100  
) a  
JOIN   
(  
    SELECT Name, MAX(AnotherId) AnotherId   
    FROM TableA   
    WHERE id = 100  
    GROUP BY Name   
) b  
on a.Name  = b.Name and a.AnotherId = b.AnotherId   

这是linq

var Collection = from R in DbContext.TableA  
join G in (DbContext.TableA.Where(r => r.Id == 100).GroupBy(r => new { r.Name, r.AnotherId } ).Select(g => new { Name = g.Name , AnotherId = g.Max(o => o.AnotherId) }))  
on new { R.Name, R.AnotherId } equals new { G.Name, G.AnotherId }  
where R.Id == 100  
select R;  

但是我遇到了编译错误,我不知道如何修复。任何想法

join子句中某个表达式的类型不正确。调用“加入”时类型推断失败。

错误7'System.Linq.IGrouping'不包含'Name'的定义,并且没有扩展方法'Name'接受类型'System.Linq.IGrouping'的第一个参数可以找到(你是否错过了使用指令或程序集引用?)

4 个答案:

答案 0 :(得分:2)

当您只想按r.Name分组时,按r.Name,r.AnotherId进行分组。

var Collection = from R in DbContext.TableA  
join G in (DbContext.TableA
                      .Where(r => r.Id == 100)
                      .GroupBy(r => r.Name)
                      .Select(g => new { Name = g.Key , AnotherId = g.Max(o => o.AnotherId) }))  
on new { R.Name, R.AnotherId } equals new { G.Name, G.AnotherId }  
where R.Id == 100  
select R; 

并使用Fluent语法

var collection = DbContext.TableA
                          .Where(t1 => t1.Id == 100)
                          .Join(DbContext.TableA
                                .Where(t2 => t2.Id == 100)
                                .GroupBy(t2 => t2.Name)
                                .Select(group => new{Name = group.Key, 
                                                      AnotherId = group.Max(e => e.AnotherId)})
                                 ),
                                 t1 => new{t1.Name, t1.AnotherId} ,
                                 t2 => new{t2.Name, t2.AnotherId},
                                 (t1, t2) => t1);

答案 1 :(得分:1)

你需要以下语法,注意添加'Key'

var Collection = from R in DbContext.TableA  
join G in (DbContext.TableA.Where(r => r.Id == 100)
            .GroupBy(r => new { r.Name, r.AnotherId } )
            .Select(g => new { Name = g.Key.Name , AnotherId = g.Max(o => o.AnotherId) }))  
on new { R.Name, R.AnotherId } equals new { G.Name, G.AnotherId }  
where R.Id == 100  
select R;  

答案 2 :(得分:1)

我建议您对查询的所有部分使用查询语法。通过这种方式,您的linq查询将具有与原始SQL查询更相似的结构。它看起来像这样:

var query =
  from a in 
    (from x in DbContext.TableA
     where x.ID == 100 
     select x)
  join b in
    (from x in DbContext.TableA
     where x.ID == 100
     group x by x.Name into x
     select new
     {
       Name = x.Key,
       AnotherId = x.Max(o => o.AnotherId),
     })
  on new { a.Name, a.AnotherId } equals new { b.Name, b.AnotherId }
  select a;

答案 3 :(得分:0)

saj和Raphael都找到了好处:

.GroupBy(r => new { r.Name, r.AnotherId } )
.Select(g => new { Name = g.Name , AnotherId = g.Max(o => o.AnotherId) }))

群组没有名称。每个组都有一个Key(Key有一个Name和AnotherId)。

由于您需要Max(AnotherId),因此您不希望在分组密钥中包含AnotherId(与原始查询的GroupBy子句中不存在相同)。

.GroupBy(r => r.Name)  //the Name is the Key
.Select(g => new { Name = g.Key, AnotherId = g.Max(o => o.AnotherId) }))