How to build a query for an unknown entity? RRS feed

  • Question

  • Hi all,

    I want to have a class that can build a "where" clause for an entity.  But I don't know the exact entity.

    Say I want to create a class named genericLookup. That class has a method named getData. 

    I then want to subclass that into clientLookup, VendorLookup, BillLookup, etc. 

    So a billLookup subclass might expect billDate, customerID, and balance as lookup values whereas a customerLookup might expect first, last, company, and address as the lookup values.

    As you can guess, there will be lots of code that is identical for the subclasses, so the right place to put it is in the genericLookup class.

    What has me stumped right now is the query to get the data.

    I have a routine in the gerericLookup that accepts the values the user whats for the query and then calls a protected method that evaluates the data and builds the appropriate where clause.  The protected method is coded in the appropriate subclass.

    In order to be able to search, I have to define a protected field in the genericLookup class that can hold a queryiable entity.  I have tried to define something using System.Data.Objects.ObjectQuery as well as just EntityObject. 

    But when I try to enter the .Where values in the protected subclass method, VS complains that my class field is not based on Linq.IQueryable....

    What would be the correct declaration for the class field so that it could hold a where clause?  Links to some examples where there are multiple .Where conditions as well would be GREATLY appreciated. 

    I have tried some searches on this but can't seem to figure out the correct syntax to get results that answer the question.

    Thanks greatly,


    (and yes, I DO mark correct answers)


    me (and yes, I DO mark correct answers)
    Tuesday, September 13, 2011 5:37 PM

All replies

  • Hi,


    Where is an extention method for Queryable and Enumerable, so you should convert your field to them.


    Have a nice day.

    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Sunday, September 18, 2011 10:15 AM
  • Alan,

    Thanks for the link.  I have been playing with it for a while now and am still a little stumped. 

    If I wanted to have a generic class, what would be the best way to define it to hold entities?  The link gave me:

    public class testGenerics<T> : List<T>

    But I am not sure what I can use to replace List.  Everything I tried resulted in an error.

    I did try this:

    			testGenerics<mastername_index> foo = new testGenerics<mastername_index>() ;
    			var results = foo.Where(x => x.Name.Last == "Smith");
    			var xxx = results.Count();

    But the results were always 0 (and we have lot's of Smiths....)

    Again, my goal is to have a property defined in a parent class, where I have code that references that property in that same class.  But in teh subclass, I actually define the correct Entity to use and have code that is specific to that one entity.

    Any ideas are greatly appreciated!





    me (and yes, I DO mark correct answers)
    Tuesday, September 20, 2011 6:26 PM