0

I have a query:

public IQueryable GetOrder(int id)
{
  using (Context = new MyEntities())
  {
    IQueryable query = Context.Orders
     .Include("Customers")
     .Include("Products")
     .Include("OrderLines")
     .Where(o => o.OrderID == id);
    return query;
  }
}

Now, I want to extract the basic query (everything sans the .Where clause) so I can reuse it in other methods. I tried this:

private IQueryable GetIncludes(MyEntities context)
{
  return context.Orders
   .Include("Customers")
   .Include("Products")
   .Include("OrderLines");
}

public IQueryable GetOldOrders()
{
  using (Context = new MyEntities())
  {
    DateTime ninetyDaysAgo = new DateTime(DateTime.Now.AddDays(-90));

    IQueryable query = GetIncludes(Context);
    query = query.Where(o => o.OrderDate < ninetyDaysAgo);
    return query;
  }
}

but the compiler tells me it Cannot convert lambda expression to type 'string' because it is not a delegate type.

I looked at Cannot convert lambda expression to type 'string' because it is not a delegate type and can report that I am using System.Linq (and System.Linq.Dynamic) and System.Data.Entity. System.Data.Entity is greyed out in VS.

How do I create an IQueryable that I can add a .Where clause to after it's created?

ASh
  • 34,632
  • 9
  • 60
  • 82
mcalex
  • 6,628
  • 5
  • 50
  • 80

1 Answers1

2

You can add a entity type into IQueryable

private IQueryable<Order> GetIncludes(MyEntities context)
{
  return context.Orders
   .Include("Customers")
   .Include("Products")
   .Include("OrderLines");
}

then update where you call this method

IQueryable<Order> query = GetIncludes(Context);
Khai Nguyen
  • 935
  • 5
  • 17