C# LINQ – Creating IQueryable Where Query with String Predicate

clinq

I would like to create a IQueryable Where method as shown below:

public static IQueryable Where(this IQueryable source, string predicate, params object[] values)
{}

So instead of Where(u => u.Points >= 20) I could pass a string predicate like Where("Points >= param0", 20)

What would be involved in accomplishing this task?

Best Answer

The code to accomplish what you are asking is large, it is easier to use NuGet package NequeoLinq. Sample code, first add the extension

using Nequeo.Extension;
using Nequeo.Linq.Extension;

On any collection type:

Where("ProOne >= @0 && ProTwo <= @1", 30, 70);

Pass the predicate as a string containing the property names of the collection type and the collection of parameters contained in the predicate (@0, @1 refer to 30 and 70).