.net expando and LINQ. Is it possible or not?

I have a simple list of expando objects called products.

I add various fields to these objects at runtime (e.g. color or size)

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

With a classic list of objects, I could write a LINQ query as follows:

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

but with expandos, how can this be achieved, knowing that I don’t know the names of the fields in advance (could it be the size of the weight or something else)?

Thanks in advance.

+6
linq expandoobject
source share
2 answers

The expando object implements IDictionary (Of String, Object) So you can send it to IDictionary and access its properties by passing a string.

+6
source share

You can write your code as follows: From point to Products Where (element as dynamic) .color = "red" select item

0
source share

All Articles