Query written on LinqPad does not run in Visual studio c# code
In the code below, the highlighted section is a database defined function that populates a list of Options for each ID.
var opps = (from o in Entities.Table1
join oa in Entities.Table2 on o.Id equals oa.Table1Id
where (o.StatusId == 1)
&& oa.UserId == userId
select new
{
ID = o.Id,
Options = Entities.GetOptions(o.Id),
LastUpdated = o.UpdatedDate
}).ToList();
This works fine in LinqPad, but when I transfer this code into a c# application it fails.
The error code is:
LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1[Dal.GetOptionsReturnModel] GetOptions(System.Nullable`1[System.Int64])' method, and this method cannot be translated into a store expression.
I have tried adding AsEnumerable() to the end of the first line:
var opps = (from o in Entities.Table1.AsEnumerable()
But this give a different error.
"System.Data.Entity.Core.EntityCommandExecutionException",
"An error occurred while executing the command definition. See the inner exception for details.",
"There is already an open DataReader associated with this Command which must be closed first.",
This is very frustrating, as up until now, I had assumed that code created in Linqpad would just work in a c# application.
Why is this happening, and how do I fix this?
var opps = (from o in Entities.Table1
join oa in Entities.Table2 on o.Id equals oa.Table1Id
where (o.StatusId == 1)
&& oa.UserId == userId
select new
{
ID = o.Id,
Options = Entities.GetOptions(o.Id),
LastUpdated = o.UpdatedDate
}).ToList();
This works fine in LinqPad, but when I transfer this code into a c# application it fails.
The error code is:
LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1[Dal.GetOptionsReturnModel] GetOptions(System.Nullable`1[System.Int64])' method, and this method cannot be translated into a store expression.
I have tried adding AsEnumerable() to the end of the first line:
var opps = (from o in Entities.Table1.AsEnumerable()
But this give a different error.
"System.Data.Entity.Core.EntityCommandExecutionException",
"An error occurred while executing the command definition. See the inner exception for details.",
"There is already an open DataReader associated with this Command which must be closed first.",
This is very frustrating, as up until now, I had assumed that code created in Linqpad would just work in a c# application.
Why is this happening, and how do I fix this?
Comments
I'm having a very similar problem...
This query in C# returns no entries
But the same query in LinqPad results in garbage SQL
If I remove the AsEnumerable() from Builds, then LinqPad generates sensible SQL, but the C# code calls it untranslatable
Any tips to narrow the gap (mainly in my understanding) would be greatly appreciated!
Cheers
John
A simpler query that works in LinqPad, but not in EF (This is returned as untranslatable and to convert to a client side query)
Which leads me to the question how does LinqPad translate its queries?
Cheers
John
LINQPad uses either LINQ-to-SQL or EF Core, depending on which option you choose when adding the connection.
It appears that EF Core does not support that kind of expression. You could reformulate it as follows:
There might be other workarounds, too - StackOverflow is likely to be a good place to look or ask.