Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "select new" and "Select(a => new"

please consider this 2 queries and their result:

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()
            });

and it's result in GridView::

enter image description here

and the second query:

 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() };

and it's result in GridView::

enter image description here

why Select(a => new) in first query show key column but select new does not show that?

like image 308
Arian Avatar asked Mar 11 '26 04:03

Arian


1 Answers

try this

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() };
like image 50
Hardrada Avatar answered Mar 12 '26 18:03

Hardrada