2016-07-13 14 views
0

Ich habe ein Problem mit der Erstellung von LINQ-Abfrage mit Lambda-Ausdruck. Ich muss zwei Tische verbinden und Bedingungen machen. Ich habe zwei Tabellen MSR und BOMDetail.LINQ JOIN mit WHERE-Bedingung

MSR hatte diese Spalten -> MSRID, PN, Käufer, Plant EditDate. BomDetail hatte diese Spalten -> BOMID, PN, AltQty, Plant, EditDate.

Und ich muss diese Abfrage in LINQ schreiben.

SELECT MSR.PN, Buyer, MSR.EditDate, MSR.Plant FROM MSR 
JOIN BomDetail bd ON MSR.PN = bd.PN AND MSR.Plant = bd.Plant 
WHERE LEN(ISNULL(bd.AltQty,''))>0 

Ich muss 2 Bedingungen machen PN muss zwischen Tabellen und Plant auch gleich. Ich habe für das Ergebnis ViewModel in asp.net MVC.

public class MSRViewModel 
{ 
    public string PN { get; set; } 
    public string Buyer { get; set; } 
    public string Plant { get; set; } 
    public DateTime EditDate { get; set; } 

} 

Und hier ist meine Probe, es funktioniert gut, aber ich weiß nicht, wo ich die zweite Bedingung für bd.Plant = MSR.Plant schreiben müssen.

var data = DbContext.BomDetails.Where(x => !string.IsNullOrEmpty(x.AltQty)) 
         .Join(DbContext.MSRs 
         , bd => bd.PN, 
         msr => msr.PN, 
         (bd, msr) => new MSRViewModel 
         { 
          PN = msr.PN, 
          Buyer = msr.Buyer, 
          Plant = msr.Plant, 
          EditDate = msr.EditDate 
         }).ToList().AsEnumerable(); 

Danke.

Antwort

1

Sie können dies wie folgt:

var data = DbContext.BomDetails.Where(x => !string.IsNullOrEmpty(x.AltQty)) 
        .Join(DbContext.MSRs 
        , bd => new { bd.PN, bd.Plant }, 
        msr => new { msr.PN, msr.Plant }, 
        (bd, msr) => new MSRViewModel 
        { 
         PN = msr.PN, 
         Buyer = msr.Buyer, 
         Plant = msr.Plant, 
         EditDate = msr.EditDate 
        }).ToList().AsEnumerable(); 
+0

Oh danke. Es funktioniert gut. Du rettest mich. – tomas