带有连接的Linq查询和给出错误的where子句

时间:2016-03-15 23:12:46

标签: c# linq

我对这个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();

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

您的查询中有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();
相关问题