0

私はこのシナリオを持っていますが、関連するSOの質問を調べても解決策が見つかりませんでした:

  (from TD as tx 
   join P as px  on tx.field1= px.ID
   join Q as rp  on tx.field2 = rp.ID
   join L as lc  on tx.field3= lc.ID
    group by  tx.field1,
              tx.field2, 
              L.randomfield4,
              ....a bunch of fields from P,Q,L
   )as groupItem
   left outer join M on groupItem.field1=M.ID
   select new { */ Elements from groupItem and M /*}

私のLinqは次のようになります:

from tx in TD 
join itemP in P on tx.field1 equals P.ID
join itemQ in Q on tx.field2 equals P.ID
join itemL in L on tx.field3 equals P.ID
 group new { tx.field1 ,tx.field2 ,L.randomfield4 } by new { **fields from tx,p,q,etc} into groupItem 
 join dM in M on ???? 

groupItemsから要素を選択しようとすると、プロパティにアクセスできません(何も選択しなかったため)。

誰かがこの問題の出発点を手伝ってくれますか?
また、質問のより良い名前で私を助けてください:)

4

1 に答える 1

2

うまくいけば、私はあなたの質問を正しく理解しましたが、ここに私の答えがあります。クエリを2つに分割したので、完全に理解できれば、それらを組み合わせるだけで簡単に理解できます。

基本的に、変数「groupedCollection」は最初の結合であり、グループ化には基本的に次のコレクションTD、P、Q、およびLが含まれます。次に、2番目の変数「finalCollection」は左外側の結合であり、必要な結果が得られます。

var groupedCollection = from tx in TD
join itemP in P on tx.field1 equals itemP.ID
join itemQ in Q on tx.field2 equals itemQ.ID
join itemL in L on tx.field3 equals itemL.ID
group new
{   
    //Select the collections you want to group it by
    P,
    Q,
    L,
    TD
}
by new
{
    //Select fields you want to output group by here
    tx.field1,
    tx.field2,
    tx.field3,
    itemL.RandomField4
}
    into g
    select new
    {
        //Use ky to get your properties as they are already grouped by
        yourField1 = g.Key.field1,
        yourField2 = g.Key.field2,
        yourField3 = g.Key.field3,
        yourField4 = g.Key.RandomField4
    };

var finalCollection = from dM in M
    //This is your Left Outer Join
    join xx in groupedCollection on dM.ID equals xx.yourField1 into sr
    from w in sr.DefaultIfEmpty()
    //Until here
    select new
    {
        finalResultID = dM.ID,
        finalField1 = w.yourField1,
        finalField2 = w.yourField2,
        finalField3 = w.yourField3,
        finalField4 = w.yourField4
    };

より多くのLINQのものについては、これはあなたにアイデアを与えるでしょう http://anyrest.wordpress.com/2010/09/27/linq-to-sql-essentials/

于 2013-02-18T22:45:00.253 に答える