1

我正在尝试为以下查询编写等效的 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”的定义,并且找不到接受“System.Linq.IGrouping”类型的第一个参数的扩展方法“Name”(您是否缺少 using 指令或装配参考?)

4

4 回答 4

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; 

并拥有流畅的语法

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);
于 2012-06-25T15:22:41.447 回答
1

ll您需要以下语法,注意添加'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;  
于 2012-06-25T15:20:30.480 回答
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;
于 2012-06-25T15:24:42.423 回答
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) }))
于 2012-06-25T15:26:10.900 回答