Is it possible to use Select (l => new {}) with SelectMany in EntityFramework

I'm trying something I'm not sure, but I want to ask here if this is possible.

Can this be done?

public IQueryable<Info> GetInfo(int count, byte languageId)
        {
            return db.Info.SelectMany(i => i.LanguageInfo)
                              .Where(l => l.Language.id == languageId)
                              .Select(l => new Info {   AddDate = l.Info.AddDate,
                                                        Description = l.Description,
                                                        EntityKey = l.Info.EntityKey,
                                                        id = l.Info.id,
                                                        Title = l.Title,
                                                        ViewCount = l.Info.ViewCount }
                                                        )
                              .OrderByDescending(i => i.id)
                              .Take(count);
        }

      

When this method is executed I got the error

Object or complex type "GuideModel.Info" cannot be built in a LINQ to Entities query.

Does this mean "impossible"?

thank

+2


source to share


3 answers


The error basically indicates that the Entity Framework does not know how to create the Info object because it is not bound to the table object. (In other words, the call to Select

c IQueryable

cannot be translated into equivalent SQL.) You could have projected Select

on the client with:



public IQueryable<Info> GetInfo(int count, byte languageId)
{
    return db.Info.SelectMany(i => i.LanguageInfo)
                      .Where(l => l.Language.id == languageId)
                      .Take(count)
                      .AsEnumerable()
                      .Select(l => new Info {   AddDate = l.Info.AddDate,
                                                Description = l.Description,
                                                EntityKey = l.Info.EntityKey,
                                                id = l.Info.id,
                                                Title = l.Title,
                                                ViewCount = l.Info.ViewCount }
                                                )
                      .OrderByDescending(i => i.id);
}

      

+3


source


Can be used Select(l => new ...)

, but not with the Entity type. You need to use anonymous type or POCO type with no parameterless constructor. Object types are "special" because of how they interact with the ObjectContext. You can select them, but not add them to the request.



+2


source


The following code worked for me. Here "SearchTerm" is a complex type. Thanks Jason :)

var lstSynonym = TechContext.TermSynonyms
                .Where(p => p.Name.StartsWith(startLetter))
                .AsEnumerable()
                .Select(u => new SearchTerm
                                 {
                                     ContentId = u.ContentId,
                                     Title = u.Name,
                                     Url = u.Url
                                 });

      

0


source







All Articles