.net expando object and LINQ. Possible or not?
Asked Answered
E

2

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.

Expressivity answered 26/3, 2011 at 22:16 Comment(0)
S
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.

Superimpose answered 26/3, 2011 at 22:25 Comment(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 !Expressivity
A
0

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

Asclepius answered 7/1, 2013 at 5:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.