LINQ to Entities does not recognize the method 'System.Object InjectFrom(System.Object, System.Object[])'
For the second version you will have to call AsEnumerable()
inside your Select
to tell clr that the method InjectFrom
will be executed in memory.
return profileEntities.Select(x => (ProfileDTO)new ProfileDTO()
{
Localizations = _repoProfileLocalization
.Query(y => y.ProfileId == x.Id)
.AsEnumerable()
.Select(y => (ProfileLocalizationDTO)new ProfileLocalizationDTO()
.InjectFrom(y))
.ToList()
}
.InjectFrom(x)).ToList();
In your first example because you instantiate a generic List inside the select, you will have all the results in memory after the select, because Linq can't execute that on the database. In the second example, Linq is still trying to do everything on the database in the select, but then you call a method which can't be translated to sql and you get the error.
LINQ to Entities does not recognize the method ToDateTime(System.String)
I Think I found the way to do it
var res = db.Table1
//other tables, and where conditions
.Select(x => new MyObj
{
//...
Imp = x.MyTable.FirstOrDefault(y => y.date_val >= System.Data.Entity.DbFunctions.AddMonths(System.Data.Entity.DbFunctions.CreateDateTime(x.year, x.month, 1, 0, 0, 0), 1)).Description
})
LINQ to Entities does not recognize the method 'Boolean ToBoolean
What you are asking for cannot be achieved by db query. I'm afraid you are stuck with in memory filtering (hope your don't have too many records) like this
return
_menuSettings.AsEnumerable().Where(...
here AsEnumerable()
will switch the context from Linq to Entities
to Linq to Objects
LINQ To Entities does not recognize the method Last. Really?
That limitation comes down to the fact that eventually it has to translate that query to SQL and SQL has a SELECT TOP
(in T-SQL) but not a SELECT BOTTOM
(no such thing).
There is an easy way around it though, just order descending and then do a First()
, which is what you did.
EDIT:
Other providers will possibly have different implementations of SELECT TOP 1
, on Oracle it would probably be something more like WHERE ROWNUM = 1
EDIT:
Another less efficient alternative - I DO NOT recommend this! - is to call .ToList()
on your data before .Last()
, which will immediately execute the LINQ To Entities Expression that has been built up to that point, and then your .Last() will work, because at that point the .Last()
is effectively executed in the context of a LINQ to Objects Expression instead. (And as you pointed out, it could bring back thousands of records and waste loads of CPU materialising objects that will never get used)
Again, I would not recommend doing this second, but it does help illustrate the difference between where and when the LINQ expression is executed.
LINQ doesn't accept static function in a predicate: LINQ to Entities does not recognize the method
Perhaps using SqlFunctions.DatePart and ISO_WEEK will help
using (var ctx = new SchedulingToolContext())
{
float qteSem = ctx.Reservations.Where(k => k.CltNameAlpha == item.CltNameAlpha
&& k.DateReservation.Year == item.DateReservation.Year
&& SqlFunctions.DatePart("isowk", this.DateReservation) == SqlFunction.DatePart("isowk", k.DateReservation)
).Sum(k => k.Qte);
var q = ctx.Quotas.Where(k => k.Id == item.IdQuota).FirstOrDefault();
return q.QteMaxJour >= item.Qte && q.QteMaxSemaine >= qteSem;
}
Error while converting nullable datetime to string in LINQ asp.net MVC
The error is pretty indicative LINQ to Entities does not support the ToString method, you should compare your dates using another approach.
Based on your code I assume you are only interested in the date part of the DateTime for comparison so I suggest you to try DbFunctions.TruncateTime Method:
Where(f => DbFunctions.TruncateTime(f.TimeStamp) == DbFunctions.TruncateTime(_temp))
Related Topics
How to Split an Ienumerable into Two by a Boolean Criteria Without Two Queries
How to Perform an Insert and Return Inserted Identity with Dapper
Is There a "Between" Function in C#
Login Using Google Oauth 2.0 with C#
How to Add a Certificate to Webclient (C#)
Methodinvoker VS Action for Control.Begininvoke
How to Tell If My Application Is Running as a 32-Bit or 64-Bit Application
Owin's Getexternallogininfoasync Always Returns Null
Can Structs Contain Fields of Reference Types
How to Get the Location of the Dll Currently Executing
Retrieving Files from Directory That Contains Large Amount of Files
How to Set Web.Config File to Show Full Error Message
What's a Good Way of Doing String Templating in .Net
Dbset.Attach(Entity) VS Dbcontext.Entry(Entity).State = Entitystate.Modified
Return List Using Select New in Linq
Add a Package with a Local Package File in 'Dotnet'
When to Use a Sortedlist<Tkey, Tvalue> Over a Sorteddictionary<Tkey, Tvalue>