6

I have a simple list of expando objects called products.

i add various fields to these objects at runtime ( for example color or size)

How can i write a LINQ query on this list based on dynamic fields ?

With a classic list of objects i could write a LINQ query like this :

From item in Products Where item.color="red" select item

but with expandos , how this can be achieved , knowing that i don't know in advance the name of the fields (it could be size of weight or anything else ) ?

Thank you in advance.

alainb
  • 185
  • 1
  • 6

2 Answers2

6

The expando object implements IDictionary(Of String, Object) Thus you could cast it to an IDictionary and access it's properties by passing a string.

Ghassen Hamrouni
  • 3,138
  • 2
  • 20
  • 31
  • 1
    Brilliant ! i was turning around for Hours and it works : from item as IDictionary(of String,Object) in products Where item("color")="Red" .... Thank You Sir ! – alainb Mar 27 '11 at 09:44
0

You can write your code like this: From item in Products Where (item as dynamic).color="red" select item