2017-05-03 5 views
1

5つの列に2つのエンティティをジョインする必要があり、SELECTの各列にGROUP BYする必要があり、各GROUP BYでCOUNTを取得する必要があります。これはSQLで私にとってはケーキですが、私はLINQのために見つけることができるすべての例と絶望的に混同しています。C#Linq複数の列の2つの表とGROUP BYの数を結合する

次の2つの質問(C# Linq Group By on multiple columns [duplicate]Group By Multiple Columns)が似ていますが、これを行う方法についてはまだ混乱しています。

var join1 = from m in context.asset 
      join o in context.organization 
      on new {hqID = a.hq_org_id, commandID = a.command_org_id, regionID = a.region_org_id, installationID = a.installation_org_id, siteID = a.site_org_id} 
      equals new {hqID = o.hq_id, commandID = o.command_id, regionID = o.region_id, installationID = o.installation_id, siteID = o.site_id} 
      select new 
      { 
       hqID = o.hq_id, 
       commandID = o.command_id, 
       regionID = o.region_id, 
       installationID = o.installation_id, 
       siteID = o.site_id 
       //hqId = o.count(org_site_id) AS count 
      }; 

私は以下のことでグループを持っている:

var group1 = from a in context.asset 
      group a by new 
      { 
       a.hq_org_id, 
       a.command_org_id, 
       a.region_org_id, 
       a.installation_org_id, 
       a.site_org_id 

       // I am not sure how to get the count 

      } into asset 
      select new 
      { 
       // I cannot figure out how to join 
      } 

GROUP BYの構文を使用して私を助けてください、私は以下の参加持っ

SELECT o.org_hq_name, 
     o.org_command_name, 
     o.org_region_name, 
     o.org_installation_name, 
     o.org_site_name, 
     o.org_subsite_name, 
     o.org_hq_id, 
     o.org_command_id, 
     o.org_region_id, 
     o.org_installation_id, 
     o.org_site_id, 
     count(org_site_id) AS count 

FROM organization o, asset a 

WHERE  o.org_hq_id = hq_org_id 
AND  o.org_command_id = a.command_org_id 
AND  o.org_region_id = a.region_org_id 
AND  o.org_installation_id = a.installation_org_id 
AND  o.org_site_id = a.site_org_id 

GROUP BY o.org_hq_name, 
     o.org_command_name, 
     o.org_region_name, 
     o.org_installation_name, 
     o.org_site_name, 
     o.org_subsite_name, 
     o.org_hq_id, 
     o.org_command_id, 
     o.org_region_id, 
     o.org_installation_id, 
     o.org_site_id 

:ここに私のSQL文です2つのテーブルのJOINではなく、2つのテーブルの複数の列で、各グループのカウントを取得します。

答えて

3

あなたのクエリは、この

var join1 = from m in context.asset 
     join o in context.organization 
     on new {hqID = a.hq_org_id, commandID = a.command_org_id, regionID = a.region_org_id, installationID = a.installation_org_id, siteID = a.site_org_id} 
     equals new {hqID = o.hq_id, commandID = o.command_id, regionID = o.region_id, installationID = o.installation_id, siteID = o.site_id} 
     group new {m,o} by new { o.org_hq_name, 
            o.org_command_name, 
            o.org_region_name, 
            o.org_installation_name, 
            o.org_site_name, 
            o.org_subsite_name, 
            o.org_hq_id, 
            o.org_command_id, 
            o.org_region_id, 
            o.org_installation_id, 
            o.org_site_id 
           } into gr 
     select new 
     { 
      org_hq_name = gr.Key.org_hq_name, 
      org_command_name = gr.Key.org_command_name, 
      org_region_name = gr.Key.org_region_name, 
      org_installation_name = gr.Key.org_installation_name, 
      org_site_name = gr.Key.org_site_name, 
      org_subsite_name = gr.Key.org_subsite_name, 
      org_hq_id = gr.Key.org_hq_id, 
      org_command_id = gr.Key.org_command_id, 
      org_region_id = gr.Key.org_region_id, 
      org_installation_id = gr.Key.org_installation_id, 
      org_site_id = gr.Key.org_site_id, 
      Count = gr.Count() 
     }; 
+0

ねえ、TriVようにLINQに書き換えることができます!私はこの質問の続編である新しい質問をしています。おそらくあなたにとっては簡単な質問になるでしょう。 http://stackoverflow.com/q/43812940/1735836 – Patricia

関連する問題