1

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

我在以下位置从“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;
            }
        }
4

1 回答 1

2

这个问题的答案很好地解释了为什么不能加入 LINQ 中的两个字段。它还建议您可以使用匿名类型进行连接,或者您可以简单地将其中一个条件移动到where子句中。他是我放在 LINQPad 中的一个快速示例,用于说明将 ajoin用于其中一个条件,将 awhere用于另一个条件,以及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);
于 2013-02-17T18:46:15.817 回答