c# - What does GROUP "x" BY "etc" do? -


i couldn't find kind of query though searched in msdn , many books. maybe here able me. here's example:

 var query = document in database.documents                                                   group document.ownerid                         new                         {                             ownername= document.owner.ownername,                            releasedate= document.releasedate,                            ownerid= document.ownerid,                            status= document.statusid                         }                        grupo                                                       select new amountofdocuments                        {                            ownername= grupo.key.ownername,                            releasedate= grupo.key.releasedate,                            ownerid= grupo.key.ownerid,                            udocuments= grupo.count(),                            sdocuments= grupo.count()                        }; 

i need make udocuments , sdocuments return number of documents status equals either u or s respectively. appreciated need figure out how query works.

thanks in advance!

check following code created in linqpad, job in 2 different ways , produce same result. second 1 preferable projects status value of grouping , counting becomes simpler

void main() {     var testlist = test.create();      var result =      testlist.groupby(x=>new {                           x.ownername,                           x.ownerid,                           x.releasedates                             })             .select(y=>new {                            y.key.ownername,                            y.key.ownerid,                            statusu = y.count(m=>m.status == "u"),                            statuss = y.count(m=>m.status == "s"),                             });      result.dump();      var result1 =      testlist.groupby(x=>new {                           x.ownername,                           x.ownerid,                           x.releasedates                             },x=>x.status)             .select(y=>new {                            y.key.ownername,                            y.key.ownerid,                            statusu = y.count(m=>m=="u"),                            statuss = y.count(m=>m=="s"),                             });      result1.dump(); }  public class test {     public string ownername {get; set;}      public int ownerid {get; set;}      public string status {get; set;}      public datetime releasedates{get; set;}      public static list<test> create()     {       return new list<test>()         {          new test         {           ownername = "abcd",           ownerid = 1,           status = "s",           releasedates = datetime.now         },         new test         {           ownername = "abcd",           ownerid = 1,           status = "s",           releasedates = datetime.now         },         new test         {           ownername = "abcd",           ownerid = 1,           status = "s",           releasedates = datetime.now         },         new test         {           ownername = "abcd",           ownerid = 1,           status = "u",           releasedates = datetime.now         },         new test         {           ownername = "abcd",           ownerid = 1,           status = "u",           releasedates = datetime.now         },         new test         {           ownername = "abcd",           ownerid = 1,           status = "s",           releasedates = datetime.now         }          };     }    } 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -