none
IQueryable<T> and IOrderedQueryable<T> without DataContext instance RRS feed

  • Question

  • Hello,

    I would like to implement IQueryable<T> without having any DataContext instance.
    I mean, I dont want to have a connection to database at this moment.

    I am working on SOA, where I have DB connection string, I have no connection so far. And I dont want to create a connection even.

    I would like to create a IQueryable<T> command and pass it to my DB layer, where it does all DataContext instance and connection for DB query.

    If Anyone knows how to use IQueryable<T> without having DataContext instance, Kindly help me on this?



    Thanks
    Hasmukh
    Monday, March 9, 2009 5:30 AM

Answers

  • You could do this if you implement your own QueryProvider and IQueryable<T>.  However, IQueryable works by containing its own execution environment (the provider), so that when you attempt to enumerate the queryable you cause it to execute.  So it is likely that your implementation of your query provider would simply be a wrapper around your DAL.  Yet, if this is the case why not use the queryable that your DAL gives you already?  If your query provider does not really do anything, and possibly throws an exception when you try to enumerate the queryable then you've subtly broken the contract for an IQueryable query.  You may want to explicitly hand the queryable to your DAL, but how would you handle the case of simple operators like Count() and First()?  They work by immediatly executing the query via the query provider.

    // find the customer given an ID
    var query = BusinessLayer.MakeQuery<Customer>();

    // oops, this fails since my stand-in queryable cannot execute itself.
    var first = query.SingleOrDefault(c => c.Id == id);


    Wayward LINQ Lacky
    Wednesday, March 11, 2009 7:42 PM
    Moderator

All replies

  • If you want to use a DAL to implement your logic, you could use Expression(TDelegate) to control your where or select or order by, which would probably be much better than to use IQueryable(T).
    Daniel - http://webs.neumont.edu/dstafford
    Monday, March 9, 2009 2:41 PM
  • Could you provide any example to use Expression for sorting.
    As I use this to provide Where condition but couldn't get any idea for sorting.

    Tuesday, March 10, 2009 6:04 AM
  • The overload for OrderBy() or OrderByDescending() is the same as for Where, so you should be able to use pretty much the same thing.
    Daniel - http://webs.neumont.edu/dstafford
    Tuesday, March 10, 2009 9:56 PM
  • You could do this if you implement your own QueryProvider and IQueryable<T>.  However, IQueryable works by containing its own execution environment (the provider), so that when you attempt to enumerate the queryable you cause it to execute.  So it is likely that your implementation of your query provider would simply be a wrapper around your DAL.  Yet, if this is the case why not use the queryable that your DAL gives you already?  If your query provider does not really do anything, and possibly throws an exception when you try to enumerate the queryable then you've subtly broken the contract for an IQueryable query.  You may want to explicitly hand the queryable to your DAL, but how would you handle the case of simple operators like Count() and First()?  They work by immediatly executing the query via the query provider.

    // find the customer given an ID
    var query = BusinessLayer.MakeQuery<Customer>();

    // oops, this fails since my stand-in queryable cannot execute itself.
    var first = query.SingleOrDefault(c => c.Id == id);


    Wayward LINQ Lacky
    Wednesday, March 11, 2009 7:42 PM
    Moderator