LINQ设计时编译错误

时间:2013-02-17 16:51:07

标签: c# linq-to-sql entity-framework-4.3

有人可以告诉我以下查询的正确语法是什么?

我从以下位置的“equals”关键字开始设计时编译错误:

&& a.applicationid equals ga.applicationid

出现以下错误: “查询正文必须以select子句或group子句结尾”

我理解错误的含义,但我看不出语法错误是什么....

public static List<ApplicationConfigurations> GetAppConfigs()
        {
            try
            {
                using (wmswebEntities DbContext = new wmswebEntities())
                {
                     IEnumerable<ApplicationConfigurations> myAppConfigs = new IEnumerable<ApplicationConfigurations>();

                     myAppConfigs = (from a in DbContext.ApplicationConfigurations
                                     join ga in DbContext.groupapplicationconfigurationslk on a.configurationid equals ga.configurationid
                                     && a.applicationid equals ga.applicationid
                                     join g in DbContext.Groups on g.groupnumber equals ga.groupnumber
                                     where a.ActiveFlag == true
                                        && ga.ActiveFlag == true
                                        && g.ActiveFlag == true
                                    select
                                        a.applicationconfigurations,
                                        g.groupnumber).ToList();

                    return myAppConfigs;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

1 个答案:

答案 0 :(得分:2)

this question的答案非常好地解释了为什么你不能加入LINQ中的两个字段。它还建议您可以使用匿名类型进行连接,也可以将其中一个条件移动到where子句中。他是我在LINQPad中放在一起的一个简单示例,用于说明其中一个条件使用join,另一个条件使用where,并使用匿名类型join

var applicationConfigs = new[] {
    new { ApplicationID = 1, ConfigurationID = 1, Name = "Application #1" },
    new { ApplicationID = 2, ConfigurationID = 1, Name = "Application #2" },
    new { ApplicationID = 3, ConfigurationID = 2, Name = "Application #3" },
    new { ApplicationID = 4, ConfigurationID = 2, Name = "Application #4" }
};
var groupApplicationConfigs = new[] {
    new { ApplicationID = 1, ConfigurationID = 1, Name = "Group App Config #1" },
    new { ApplicationID = 1, ConfigurationID = 1, Name = "Group App Config #2" },
    new { ApplicationID = 2, ConfigurationID = 1, Name = "Group App Config #3" },
    new { ApplicationID = 3, ConfigurationID = 1, Name = "Group App Config #4" }
};

//JOIN + WHERE
var q = from a in applicationConfigs
        join ga in groupApplicationConfigs 
            on a.ApplicationID equals ga.ApplicationID 
        where a.ConfigurationID == ga.ConfigurationID
        select a;
Console.WriteLine(q);

//ANONYMOUS TYPE
var r = from a in applicationConfigs
        join ga in groupApplicationConfigs 
            on new { a.ApplicationID, a.ConfigurationID } equals 
               new { ga.ApplicationID, ga.ConfigurationID }
        select a;
Console.WriteLine(r);