簡體   English   中英

使用linq連接使用兩個表在兩個表上

[英]using linq with join using two where on two tables

我正在使用Csharp Linq創建以下報告

我有兩張桌子如下

#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

該報告包含

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

我嘗試了以下,使用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();

在第二個'where'顯示錯誤

謝謝你的幫助

交換過濾並連接部分查詢並將tid重命名為t.tid或其他所需的過濾子句(在您的示例中,結果表確實具有tid == "T1"事務,但您嘗試使用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();

連接部分不能分開,因此where完成與on子句的join之前,您無法寫入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM