1

I'm building a search which will search multiple tables in our database, based on configuration. I'm getting the expression tree built, but when it comes to building the Expression Lambda, I'm getting stuck. I have:

var condExpr = Expression.Lambda<Func<Client, bool>>(expression, param);

But I don't know compile time whether I'm looking at a Client, Supplier, or some other entity. The application has plugins, so even the types that are available are unknown at compile time.

I do have a System.Type for the entity. How can I derive the generic type from that?

p.s. I have tried

var condExpr = Expression.Lambda(expression, param);

and it didn't work.

condExpr gets passed to the IQueryable result in the form of:

foreach (var b in dataResult.Where(condExpr))

Richard C
  • 2,176
  • 5
  • 30
  • 40
  • So you want to invoke `Expression.Lambda>(...`? How would `condExpr` be used? – Yacoub Massad Apr 23 '16 at 12:30
  • I'm not with my 'tools' atm. - but that's a bit more complex story with LINQ as I remember - take a look at this answer of mine (http://stackoverflow.com/a/15990676/417747) - where I elaborated a pretty complex scenario (selecting by property names), but inside should have all the nuts and bolts (much more likely) on how to wire up the lambda properly - also with a pointer to the source used and w/ all due acknowledgements to that – NSGaga-mostly-inactive Apr 23 '16 at 12:55
  • What is the type of `dataResult` (guess it's `IQueryable`)? `condExpr` must match the type `Expression.Lambda>` where `TDataResult` is the type of `dataResult`, otherwise you cannot use it in the `Where` clause. – hillin Apr 23 '16 at 12:56
  • I'm not sure what you're doing, but my guess is you may be borrowing trouble. However, you can get the generic type arguments easily: [System.Type.GetGenericArguments()](https://msdn.microsoft.com/en-us/library/system.type.getgenericarguments(v=vs.110).aspx). N.B., I've tried programatically instantiating arbitrary generics, and I didn't enjoy it. But you're not doing that. – 15ee8f99-57ff-4f92-890c-b56153 Apr 23 '16 at 13:41
  • 2
    Can you please provide a [mcve]? – Enigmativity Apr 23 '16 at 14:13

0 Answers0