Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use LINQ with dynamic collections

Tags:

c#

linq

People also ask

Can you use LINQ on dynamic?

The Dynamic LINQ library let you execute query with dynamic string and provide some utilities methods such as ParseLambda , Parse , and CreateClass .

What collections can LINQ be used with?

You can use LINQ to query any enumerable collections such as List<T>, Array, or Dictionary<TKey,TValue>. The collection may be user-defined or may be returned by a . NET API.

What is Dynamic LINQ?

The Dynamic LINQ library exposes a set of extension methods on IQueryable corresponding to the standard LINQ methods at Queryable, and which accept strings in a special syntax instead of expression trees.

What is Dynamic Query in C#?

Instead of using language operators or type-safe lambda extension methods to construct your LINQ queries, the dynamic query library provides you with string-based extension methods that you can pass any string expression into.


So long as data is an IEnumerable of some kind, you can use:

var a = ((IEnumerable) data).Cast<dynamic>()
                            .Where(p => p.verified);

The Cast<dynamic>() is to end up with an IEnumerable<dynamic> so that the type of the parameter to the lambda expression is also dynamic.


Try casting to IEnumerable<dynamic>

((IEnumerable<dynamic>)data).Where(d => d.Id == 1);

This approach is 4x faster than other approachs.

good luck