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; }
}