2017-10-24 7 views
1

私は例えば異なるテーブルLINQクエリ

からの値に基づいてテーブルを結合するために、書き込みC#のLINQをしようとしています、私は以下のように2つのテーブル表1と表2を持っています

from p in context.Table1 
        join label in context.Table2 on new 
        { 
         p.StatusId, 
         p.SubStatusId,      
         I = p.Dubs== 0, 
         J = p.Dubs> 0 
        } equals 
         new 
         { 
          label.StatusId, 
          label.SubStatusId,       
          I = label.type== 1 
          J = label.type== 2 
         } into gj 
        from label in gj.DefaultIfEmpty() 

上記のコードは、4つの値のプロパティで2つのテーブルを結合するが、私は次のようさて、私はStatusId and SubstatusId列で両方のテーブルを結合しようとしています

Table1 

Id Name Address StatusId SubStatusId Dubs 
1 ABC XYZ  1   39   10 
2 PQR XYZ  1   39   0 
3 ME WWW  2   39   5 
4 YOU XYZ  1   22   0 
5 HE XYZ  2   22   5 
6 SHE WWW  2   41   0 
7 XAZ XYZ  1   39   10 

Table2 
Id StatusId SubStatusId Status SubStatus Type 
1 1   39   Dispense Ready  1 
2 1   39   Fill  Ready  2 
3 2   22   Ship  Active 0 
4 2   41   Del  Pending 0 
5 1   22   Verify Pending 0 
6 2   39   Benefit None  0 

Table2内の行の種類は、私が参加するの外に、より複雑な述語を続けるだろう

Status SubStatus 
Fill  Ready (1,39 Since Dubs>0 for which means should return row with type 2) 
Dispense Ready (1,39 Since Dubs=0 for which means should return row with type 1) 
Benefit None (2, 39 Since type=0, this should ignore Dubs) 
Verify Pending (same as above) 
Ship  Active 
Del  Pending 
Fill  Ready (1,39 Since Dubs>0 for which means should return row with type 2) 
+0

は、それはあなたがすることに参加したいものをフォローするのは難しい、あなたが参加したデータセットがどのように見えるかの例を与えることができますか? –

+0

@AaronRoberts予想される出力を追加 – DoIt

答えて

1

以下のような結果が見えDubs

にあるもの値ゼロ該当事項はありませんとき、IとJを除外したいと思います:

from p in context.Table1 
join label in context.Table2 on new 
{ 
    p.StatusId, 
    p.SubStatusId,      
} equals new 
{ 
    label.StatusId, 
    label.SubStatusId,       
} into gj 
from label in gj.DefaultIfEmpty() 
where label.Type == 0 
    || label.Type == (p.Dubs == 0 ? 1 : 2) 
select ... 
0

ここで、結合構文はWhereの内部で制御しやすくなります。 whereはあなたのものと一致しませんが、これに基づいて複雑な節をより簡単に構築できるはずです。

var aresult = from a in ta.AsEnumerable() 
       from b in tb.AsEnumerable() 
      .Where(b => a.Field<string>("StatusId") == b.Field<string>("StatusId") 
         && a.Field<string>("SubStatusId") == b.Field<string>("SubStatusId")) 

       select new object[] { a.Field<string>("Name") 
            ,b.Field<string>("Status") 
            ,b.Field<string>("SubStatus") 
       }; 

与えられたテーブルを生成するコード...

DataTable ta = new DataTable(); 
    ta.Columns.Add("Id"); 
    ta.Columns.Add("Name"); 
    ta.Columns.Add("Address"); 
    ta.Columns.Add("StatusId"); 
    ta.Columns.Add("SubStatusId"); 
    ta.Columns.Add("Dubs"); 

    DataTable tb = new DataTable(); 
    tb.Columns.Add("Id"); 
    tb.Columns.Add("StatusId"); 
    tb.Columns.Add("SubStatusId"); 
    tb.Columns.Add("Status"); 
    tb.Columns.Add("SubStatus"); 
    tb.Columns.Add("Type"); 

    string[] tas = 
    { 
     "1,ABC,XYZ,1,39,10" 
     ,"2,PQR,XYZ,1,39,20" 
     ,"3,ME,WWW,2,39,0" 
     ,"4,YOU,XYZ,1,22,2" 
     ,"5,HE,XYZ,2,22,5" 
     ,"6,SHE,WWW,2,41,0" 
    }; 
    string[] tbs = 
    { 
     "1,1,39,Dispense,Ready,1" 
     ,"2,2,39,Fill,Ready,2" 
     ,"3,2,22,Ship,Active,0" 
     ,"4,2,41,Del,Pending,0" 
     ,"5,1,22,Verify,Pending,0" 
    }; 
    foreach (string t in tas) 
    { 
     string[] x = t.Split(','); 
     DataRow row = ta.NewRow(); 
     for (int i = 0; i < x.Length; i++) row[i] = x[i]; 
     ta.Rows.Add(row); 
    } 
    foreach (string t in tbs) 
    { 
     string[] x = t.Split(','); 
     DataRow row = tb.NewRow(); 
     for (int i = 0; i < x.Length; i++) row[i] = x[i]; 
     tb.Rows.Add(row); 
    }