A store expression cannot be turned into a method.

c# entity-framework linq-to-entities repository-pattern

Question

When I use Entity Framework, the code I saw work with LINQ to SQL fails with the following message:

LINQ to Entities does not recognize the method 'System.Linq.IQueryable'1[MyProject.Models.CommunityFeatures] GetCommunityFeatures()' method, and this method cannot be translated into a store expression.`

This is the repository code:

public IQueryable<Models.Estate> GetEstates()
{
    return from e in entity.Estates
           let AllCommFeat = GetCommunityFeatures()
           let AllHomeFeat = GetHomeFeatures()
           select new Models.Estate
                      {
                                EstateId = e.EstateId,
                                AllHomeFeatures = new LazyList<HomeFeatures>(AllHomeFeat),
                                AllCommunityFeatures = new LazyList<CommunityFeatures>(AllCommFeat)
                      };
}

public IQueryable<Models.CommunityFeatures> GetCommunityFeatures()
{
    return from f in entity.CommunityFeatures
           select new CommunityFeatures
                      {
                          Name = f.CommunityFeature1,
                          CommunityFeatureId = f.CommunityFeatureId
                      };
}

public IQueryable<Models.HomeFeatures> GetHomeFeatures()
{
    return from f in entity.HomeFeatures
           select new HomeFeatures()
           {
               Name = f.HomeFeature1,
               HomeFeatureId = f.HomeFeatureId
           };
}

LazyList is a List that increases IQueryable's power.

Could someone please explain the cause of this error?

1
88
6/19/2013 7:32:11 PM

Accepted Answer

Reason: By design, Entities using LINQ necessitates the conversion of the whole LINQ query expression into a server query. Before the query is translated, only a small number of uncorrelated subexpressions (parts of the query that do not rely on server responses) are assessed on the client. GetHomeFeatures() and other arbitrary method invocations without a known translation are not supported.
To be more precise, only constructors with no parameters and Initializers are supported by LINQ to Entities.

Solution: Therefore, instead of simply using methods from inside the LINQ query for GetCommunityFeatures() and GetHomeFeatures(), you must merge your sub query into the main one to get around this error. Additionally, there is a problem with the lines in which you attempted to create a new instance of LazyList using its parameterized constructors, as you may have done in SQL to LINQ. To address this, switching to client evaluation of LINQ queries would be the answer (LINQ to Objects). For your LINQ to Entities queries, you will need to run the AsEnumerable method first before invoking the LazyList constructor.

This kind of stuff ought to work:

public IQueryable<Models.Estate> GetEstates()
{
    return from e in entity.Estates.AsEnumerable()
       let AllCommFeat = from f in entity.CommunityFeatures
                         select new CommunityFeatures {
                             Name = f.CommunityFeature1,
                             CommunityFeatureId = f.CommunityFeatureId
                         },
       let AllHomeFeat = from f in entity.HomeFeatures
                         select new HomeFeatures() {
                             Name = f.HomeFeature1,
                             HomeFeatureId = f.HomeFeatureId
                         },
       select new Models.Estate {
            EstateId = e.EstateId,
            AllHomeFeatures = new LazyList<HomeFeatures>(AllHomeFeat),
            AllCommunityFeatures = new LazyList<CommunityFeatures>(AllCommFeat)
       };
}


More details Please visit What LINQ to Entities functionality is lacking? for additional information. For a thorough discussion of the solutions, see also Workarounds for what LINQ to Entities does not provide. Due to the downtime of the original website, these URLs are cached copies of that page.

113
2/9/2015 11:39:59 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow