0

I'm working in Entity Framework 5 and having problems creating an expression to use inside a method.

I believe the problem is that normally I would call the expression in a lambda expression such as dbContext.Counties.Select(GetLargeCities()), but in the code I am working with, I am projecting the Counties entity into a view model called CountyWithCities. Where I would normally call the expression, I have a singleton c and don't know how to call the expression there.

The reason I want to accomplish this using an expression is because I want the GetCountiesWithCities method to hit the database once, with Entity Framework constructing a complex graph for all the objects in the result.

For some reason the code below is producing the error `The name 'GetLargeCities' does not exist in the current context."

    public class CountyWithCities // this is a view model
    {
        public int CountyID { get; set; }
        public string CountyName { get; set; }
        public IEnumerable<City> Cities { get; set; }
    }

    public class City // this is an entity
    {
        public int CityID { get; set; }
        public string CityName { get; set; }
        public int Population { get; set; }
    }

    public IEnumerable<CountyWithCities> GetCountiesWithCities(int StateID)
    {
        return dbContext.States
            .Where(s => s.StateID = StateID)
            .Select(s => s.Counties)
            .Select(c => new CountyWithCities
            {
                CountyID = c.CountyID,
                CountyName = c.CountyName,
                Cities = GetLargeCities(c) // How do I call the expression here?
            });
    }

    public Expression<Func<County, IEnumerable<City>>> GetLargeCities = county =>
        county.Cities.Where(city => city.Population > 50000);

Thanks!

1 Answer 1

1

I normally do this with an extension method.

public static IQueriable<City> LargeCities(this IQueriable<County> counties){
    return counties
        .SelectMany(county=>county.Cities.Where(c=>c.Population > 50000));
}

usage:

dbContext.Counties.LargeCities()


   public IEnumerable<CountyWithCities> GetCountiesWithCities(int StateID)
    {
        return dbContext.States
            .Where(s => s.StateID = StateID)
            .Select(s => s.Counties)
            .LargeCities()
            .GroupBy(c=>c.County)
            .Select(c => new CountyWithCities
            {
                CountyID = g.Key.CountyID,
                CountyName = g.Key.CountyName,
                Cities = g.AsQueriable() // i cant remember the exact syntax here but you need the list from the group
            });
    }
5
  • Luke, I tried you answer, but it throws LINQ to Entities does not recognize the method 'System.Collections.Generic.IEnumerable`1[MyData.County] GetLargeCities()' method, and this method cannot be translated into a store expression. Perhaps my problem is that I am attempting to use the method inside the projection .Select(c => new CountyWithCities { }). I also noticed that despite typing the extension method as IQueryable<City> the error says the method is returning IEnumerable<City>.
    – Kuyenda
    Oct 17, 2013 at 21:38
  • Quoting this answer (stackoverflow.com/a/4044751/125292) "Compiled functions can't be converted to SQL, only expressions can." So I don't think an extension method would work here. I wish that were not the case.
    – Kuyenda
    Oct 17, 2013 at 22:08
  • You cant call it in the middle of a lambda but you can use it as part of a chain of queries which will be executed at once. Ive included a sugguested way of using this (its off the top of my head so syntax ext may not be quite right)
    – undefined
    Oct 17, 2013 at 22:37
  • Luke, I'm specifically looking for a way of calling an expression from within the middle of a lambda. On the other hand, you're answer showed me how to maintain the IQueryable across chainable methods (this IQueryable<T> source). I appreciate your help.
    – Kuyenda
    Oct 17, 2013 at 22:41
  • @Kuyenda the only way i know of to actually call an expression inside a lambda is with a reusable expression tree. Linqkit allows this but its a bit of work
    – undefined
    Oct 17, 2013 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.