"select new" 和 "Select(a => new" 的区别是什么?

3

请考虑以下两个查询及其结果:

var result = ent.tblCustomGroupBies
            .GroupBy(a => groupA.Contains(a.Group.Value) ? "A" :
                          groupB.Contains(a.Group.Value) ? "B" :
                          "N/a")
            .Select(a => new
            {
                KEY = a.Key,
                VALUE = a.Count()
            });

并且其结果显示在GridView中:

图片描述

第二个查询:

 var result3 = from p in ent.tblCustomGroupBies
               group p by new { Criterion = groupA.Contains(p.Group.Value) ? "A" : 
                                            groupB.Contains(p.Group.Value) ? "B" : 
                                            "N/a" }
               into g
               select new { KEY = g.Key, VALUE = g.Count() };

并且它的结果在 GridView 中显示如下:

enter image description here

为什么第一个查询中的 Select(a => new) 显示键列,但 select new 却没有显示?


3
产生你的输出的代码在哪里? - Andrew Barber
在调试模式下观察变量时,两个变量内的结果是否相同?你如何将它们可视化呢? - Mohnkuchenzentrale
1个回答

2
尝试这个。
var result3 = from p in ent.tblCustomGroupBies
               group p by new { Criterion = groupA.Contains(p.Group.Value) ? "A" : 
                                            groupB.Contains(p.Group.Value) ? "B" : 
                                            "N/a" }
               into g
               select new { KEY = g.Key.Criterion, VALUE = g.Count() };

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接