2017-10-24 3 views
1

I write C# LINQ ist versucht, eine Tabelle auf den Werten aus einer anderen TabelleLINQ-Abfrage für eine Tabelle Gruppierung basierend auf den Werten aus einer anderen Tabelle

Zum Beispiel basierte zu verbinden, habe ich zwei Tabellen Tabelle 1 und Tabelle 2, wie unten

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 

Jetzt versuche ich, beide Tabellen mit StatusId and SubstatusId Spalten zu verbinden wie folgt

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

Der obige Code verbinden zwei Tabellen, die von vier Werte Eigenschaften, aber ich möchte ich und J auszuschließen, wenn Art der Zeile in Table2 Null keine Sachverhalte bekannt ist, welchen Wert in Dubs ist

Ergebnis sieht aus wie unten

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

Es ist schwer zu folgen, was Sie sein anschließen möchten, können Sie ein Beispiel dafür, was das zusammengefügte Datensatz aussehen sollte? –

+0

@AaronRoberts Hinzugefügte erwartete Ausgabe – DoIt

Antwort

1

ich die komplexeren Prädikate aus der Verbindung halten würde:

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

Die Join-Syntax scheint leichter in einem Where zu steuern. Die Stelle passt nicht zu Ihrer, aber Sie sollten in der Lage sein, die darauf basierende komplexe Klausel einfacher zu erstellen.

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

-Code-Tabellen zu erzeugen gegeben ...

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); 
    } 
Verwandte Themen