1

I've to create method which returns IQueryable, and tor depends on exact type. Is it possible to prepare any criteria which could be used after WHERE statement to get that?

for example, if T == License i use "c.fkCustomer == Organization.Customer"

if T== People , I use "c.fkPeople== Organization.People" etc.

XPQuery<T> cQuery = new XPQuery<T>(cSession);
IQueryable CurrQr = from c in cQuery 
                    where "c.fkCustomer == Organization.Customer" 
                    select c;

Can someone suggest something, how to achieve this goal?

2 Answers 2

4

I think you would be better off to use a lambda as an argument here rather than dynamic linq eg.

public IQueriable<T> MyQuery<T>(Expression<Func<T, bool>> predicate)
{
    return new XPQuery<T>(cSession).Where(predicate)/*and any other bits you want at the moment this is a straight up where clause so kinda pointless*/;
}

then you can call it with:

MyQuery(c=> c.fkCustomer == Organization.Customer)

or

MyQuery(c=> c.fkPeople == Organization.People)
2
  • there was small mistake in my code: there should be "IQueryable CurrQr = from c in cQuery... "
    – Edgar
    Dec 22, 2012 at 9:49
  • @konopiuxkonopiux ive updated this with what i think you are meaning
    – undefined
    Dec 22, 2012 at 10:46
1

Yes, this can be done. One way to do this is to use the Dynamic Query Library which you can find here along with detailed information on how to use it: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.