1

Can someone please inform me what the correct syntax is for the below query?

I get a design time compile error beginning at the "equals" keyword at the following spot:

&& a.applicationid equals ga.applicationid

with the following error: "A query body must end with a select clause or group clause"

I understand what the error means, but I can't see what the syntax error is....

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;
            }
        }
rsbarro
  • 27,021
  • 9
  • 71
  • 75
sagesky36
  • 4,542
  • 19
  • 82
  • 130

1 Answers1

2

The answer to this question has a very good explanation of why you cannot join on two fields in LINQ. It also suggests that you can either use an anonymous type to do the join, or you can simply move one of the conditions into the where clause. He's a quick example I put together in LINQPad to illustrate using a join for one of the conditions and a where for the other, and using join with an anonymous type.

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);
Community
  • 1
  • 1
rsbarro
  • 27,021
  • 9
  • 71
  • 75