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

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.

"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?

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!