2013-05-28 14 views
5

sto usando Csharp Linq per creare il seguente rapportoutilizzando LINQ con join utilizzando due dove su due tavoli

Ho due tabelle come di seguito

 

#Users 
nid  pid  name 
1  1  name1 
2  1  name2 

#Transactions 
nid  tid  location dcode 
1  T1  L1   D1 
2  T1  L2   D1 
2  T2  L1   D1 
2  T2  L3   D1 

Il rapporto contiene

 

    a) columns from users table where nid != pid 
    b) columns from transactions where tid == T2 and nid = results from a) 
    c) the combination can have only one top row in result 

nid  name  tid  Location 
2  name2  T2  L1 

the second record will not be present 
- 2  name2  T2  L3 

Ho provato il seguente, utilizzando join

var report = (from u in users where u.nid != u.pid 
         join t in transactions 
         where t.tid == "T2" 
         on u.nid equals t.nid 
         select new 
         { 
         // the report columns 
         }).Distinct().ToList(); 

sul tavolo secondo 'dove' un errore viene visualizzato

grazie per qualsiasi tipo di assistenza

risposta

2

filtraggio Swap e parti di unione della query e rinominare tid in t.tid o altra clausola di filtraggio desiderato (nel tuo esempio risultante fa avere rapporti con tid == "T1", ma si tenta di filtrare con T2):

var report = (from u in users  
       join t in transactions 
       on u.nid equals t.tid  //<-- this line should precede 
       where t.tid == "T2"  //<-- this one 
       && u.nid != u.pid 
       select new 
       { 
        // the report columns 
       }).Distinct().ToList(); 

registrazione parti non possono essere separati, quindi non è possibile scrivere where fino a quando non finito join con la clausola on.

+0

sì, l'errore è scomparso, lo contrassegnerò come risposta dopo 8 minuti .. grazie ancora – arvind

Problemi correlati