0

I am using a delegate to construct the where clause of a linq query. Here is an example:

public Func<Order, bool> WhereFunc(string search)
{
   return o => o.OrderNumber.Contains(search) || o.Part.Name.Contains(search);
} 

I then call this function like so:

entities = _orderRepo.Query
                        .Where(WhereFunc(search))
                        .OrderBy(OrderByFunc(orderBy))
                        .Skip(start*pageSize)
                        .Take(pageSize)
                        .ToList()
                        .Select(OrderDisplay.New);

I get the following error being given from executing the WhereFunc:

{"There is already an open DataReader associated with this Command which must be closed first."}

Could anyone help me understand why this is happening and what I can do to mitigate it? I assume it's something to do with accessing the proxy entities in the delegate function but I'm not exactly sure...

UPDATE

If I change WhereFunc to the following:

public Func<Order, bool> WhereFunc(string search)
{
   return o => o.OrderNumber.Contains(search);
} 

Then then exception is not thrown - hence my assumption it's something to do with proxy entities.

UPDATE

Here are my entities:

public class Order : EntityBase<int>
{
    public string OrderNumber { get; set; }
    public virtual Part Part { get; set; }

    [ForeignKey("Part")]
    public int PartId { get; set; }

}

public class Part : EntityBase<int>
{
    public string Name { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
}
Alex
  • 1,322
  • 1
  • 20
  • 44
  • Are you sure that is happening because of the delegates? Does it also happen if you remove the `Where` and `OrderBy` clause? – Daniel Hilgarth Jun 24 '15 at 11:23
  • The quick fix for that error is usually to enable MultipleActiveResultSets in the connection string, but first read up on what it does to determine whether that's appropriate for you. https://msdn.microsoft.com/en-us/library/h32h3abf(v=vs.110).aspx – Andy Nichols Jun 24 '15 at 11:26
  • @AndyNichols I'd always advise one fix the bug rather than use MARS. If you don't know what's causing the problem, then you've still got behaviour you don't understand *and* the complications that MARS can bring. – Jon Hanna Jun 24 '15 at 11:30
  • The exception means it tried to hit the database on a connection it was still using to hit the database. I'd guess that perhaps the mapping to the `Part` property wasn't correct so it had to retrieve the property separately, but without seeing more that's just an educated guess. – Jon Hanna Jun 24 '15 at 11:32
  • @DanielHilgarth see update. – Alex Jun 24 '15 at 11:33
  • I really question why you even need `WhereFunc()` and `OrderByFunc()` to be honest (just put the lambda expressions inline). By that's just an opinion. As for your issue, check out this [SO](http://stackoverflow.com/questions/6062192/there-is-already-an-open-datareader-associated-with-this-command-which-must-be-c) question, it might help. – Jason Evans Jun 24 '15 at 11:33
  • 1
    Have you tried moving the logic out of "WhereFunc" and placing it directly into the "Where" clause? I've never seen this kind of error before and I use LINQ quite often, but I've also never separated the logic like that. Actually, now that I'm thinking about it, what items are you getting from _orderRepo.Query? What is _orderRepo? Have you tried getting those items and assigning them to a temporary variable, then running your LINQ functions on that variable? – K_Ram Jun 24 '15 at 13:30

0 Answers0