【问题标题】:Linq query with a join and a where clause giving an error带有连接和 where 子句的 Linq 查询给出错误
【发布时间】:2016-03-15 23:12:46
【问题描述】:

我对这个 Linq 查询做错了什么?

sql语句为:

SELECT 
    * 
FROM 
    ReportGroups rg INNER JOIN ReportDefinitions rd on rg.ReportGroupID = rd.ReportGroupID
WHERE 
    rd.ReportGroupID = 5

LINQ:

var query = from rg in ReportGroups  
                join rd in ReportDefinitions on rg.ReportGroupID equals rd.ReportGroupID
                .Where rd.ReportGroupID equals 5
                 select new
                {
                    rg.ReportGroupName, 
                    rd.ReportName
                };

    query.Dump();

【问题讨论】:

  • 使用 where 和 == ,允许使用 where 的变体,但应该适合你

标签: c# linq


【解决方案1】:

您的查询中有 2 个错误。

首先使用where 而不是.Where

其次在where子句中不要使用equals使用==equals仅用于joins

像这样:

var query = from rg in ReportGroups  
                join rd in ReportDefinitions on rg.ReportGroupID equals rd.ReportGroupID
                where rd.ReportGroupID == 5 //updates here
                 select new
                {
                    rg.ReportGroupName, 
                    rd.ReportName
                };

    query.Dump();

【讨论】:

    【解决方案2】:

    您似乎将查询理解语法与方法调用语法混合在一起。尝试将.Where 替换为where

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      相关资源
      最近更新 更多