【问题标题】:using linq with join using two where on two tables使用 linq 和 join 在两个表上使用两个 where
【发布时间】:2013-05-28 18:07:14
【问题描述】:

我正在使用 Csharp Linq 创建以下报告

我有两张如下表

#用户 nid pid名称 1 1 姓名1 2 1 姓名2 #交易 nid tid 位置 dcode 1 T1 L1 D1 2 T1 L2 D1 2 T2 L1 D1 2 T2 L3 D1

报告包含

a) 用户表中 nid != pid 的列 b) tid == T2 和 nid = 结果的事务的列 a) c) 组合在结果中只能有一个顶行 nid 名称 tid 位置 2 名称2 T2 L1 第二条记录将不存在 - 2名2 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();

在第二个“位置”显示错误

感谢您的帮助

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    交换过滤和连接部分查询并将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

    【讨论】:

    • 是的,错误已经消失,我会在8分钟后将其标记为答案..再次感谢
    猜你喜欢
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 2016-10-19
    • 2021-04-01
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多