none
type arguments for method System.Linq.Queryable.Thenby cannot be inferred RRS feed

  • Question

  • I have a COMPANY table with a one-to-many relationship to a COMPANY_SITE table. I have a linq to entities query that joins them and a foreach loop that builds a simple drop down list that displays the company name concatenated with the site name and allows the user to select the appropriate site. At the moment it looks like this:

            foreach (KQUPData.COMPANY company in dataContext.COMPANies.Include("COMPANY_SITE"))
            {
              foreach (KQUPData.COMPANY_SITE site in company.COMPANY_SITE)
              {
                ddlCompanySite.Items.Add(new ListItem((company.NAME + ": " + site.SITE_NAME), site.ID.ToString()));
              }
            }
    
    

    Now I want to be able to sort the results so that the data in the drop down list is sorted first by company name and then by site name. The sort by company name works fine.

            foreach (KQUPData.COMPANY company in dataContext.COMPANies.Include("COMPANY_SITE").OrderBy("it.NAME"))
            {
              foreach (KQUPData.COMPANY_SITE site in company.COMPANY_SITE)
              {
                ddlCompanySite.Items.Add(new ListItem((company.NAME + ": " + site.SITE_NAME), site.ID.ToString()));
              }
            }
    
    

    But I can't get the sort by SITE_NAME to work. Either of the following results in the compiler error: The type arguments for method 'System.Linq.Queryable.ThenBy<TSource,TKey>(System.Linq.IOrderedQueryable<TSource>, System.Linq.Expressions.Expression<System.Func<TSource,TKey>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

            foreach (KQUPData.COMPANY company in dataContext.COMPANies.Include("COMPANY_SITE").OrderBy("it.NAME").ThenBy("it.COMPANY_SITE.SITE_NAME"))
            {
              foreach (KQUPData.COMPANY_SITE site in company.COMPANY_SITE)
              {
                ddlCompanySite.Items.Add(new ListItem((company.NAME + ": " + site.SITE_NAME), site.ID.ToString()));
              }
            }
    
    

    Or

            foreach (KQUPData.COMPANY company in dataContext.COMPANies.Include("COMPANY_SITE").OrderBy("it.NAME"))
            {
              foreach (KQUPData.COMPANY_SITE site in company.COMPANY_SITE.OrderBy("it.SITE_NAME"))
              {
                ddlCompanySite.Items.Add(new ListItem((company.NAME + ": " + site.SITE_NAME), site.ID.ToString()));
              }
            }
    
    

    I have tried reading the documentation on both the OrderBy method and the ThenBy method and I confess I am just not getting it. I'm at a loss as to how syntactically to supply the compiler with the information it is lacking.

    Any help greatly appreciated.

    Zoe



    Monday, June 20, 2011 12:10 PM

Answers

  • Hello,

    EF doesn't support direct sorting related entities. If you want to sort them you must use projection:

    var query = dataContext.COMPANies.OrderBy(c => c.NAME)
                      .Select(c => new 
                         {
                            Company = c,
                            Sites = c.COMPANY_SITE.OrderBy(s => s.SITE_NAME)
                          });
                                   
    

    This will return collection of anonymous types ordered by company's name and each item in the collection will contain company and ordered sites.

    Best regards,
    Ladislav

    • Proposed as answer by Alan_chenModerator Tuesday, June 21, 2011 6:00 AM
    • Marked as answer by Zoe Hart Tuesday, June 21, 2011 11:23 AM
    Monday, June 20, 2011 1:43 PM

All replies

  • Hello,

    EF doesn't support direct sorting related entities. If you want to sort them you must use projection:

    var query = dataContext.COMPANies.OrderBy(c => c.NAME)
                      .Select(c => new 
                         {
                            Company = c,
                            Sites = c.COMPANY_SITE.OrderBy(s => s.SITE_NAME)
                          });
                                   
    

    This will return collection of anonymous types ordered by company's name and each item in the collection will contain company and ordered sites.

    Best regards,
    Ladislav

    • Proposed as answer by Alan_chenModerator Tuesday, June 21, 2011 6:00 AM
    • Marked as answer by Zoe Hart Tuesday, June 21, 2011 11:23 AM
    Monday, June 20, 2011 1:43 PM
  • This worked perfectly though I still have a ways to go in learning to make sense of the method-based syntax with the lambdas. Thanks for your help.
    Tuesday, June 21, 2011 11:24 AM