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 to share