“选择新”和“选择(a =>新”)之间的区别

时间:2012-11-08 06:00:36

标签: c# linq entity-framework linq-to-entities group-by

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

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 ::

enter image description here

和第二个查询:

 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没有显示?

1 个答案:

答案 0 :(得分: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() };