Remember those old posts on Dynamic LINQ? You are probably aware that Microsoft has made its implementation available as a Nuget package, but, like I said, you already have it in your machine, hidden inside the System.Web.Extensions assembly.

In order to make it easier to use, I wrote a simple extension method that works with plain old IQueryable<T>. And here it is:

params Object[] values)
   2: {
typeof(UpdatePanel).Assembly;
);
typeof(Boolean));
as Expression<Func<T, Boolean>>;
   7:  
return (query.Where(expression));
   9: }

It even supports parameters! Just two simple examples – I am using Entity Framework, but you can use whatever you like, this is totally generic:

//without parameters
).ToList();
   3:  
//with parameters
, 100).ToList();

To make it clear, all parameters must be indicated as @0, @1, and so on. It is better to use them, because the database engine can reuse the execution plan.

There’s one limitation, though: you can’t compare each value on its own, you have to specify one of its properties. For example, you can’t have:

).ToList();

The @it parameter is not recognized, which is a pity.

Make sure you have references to both System.Web and System.Web.Extensions, this is required, and won’t affect anything.

As always, glad to be of service! 动态linq表达式新方法,Dynamic LINQ Extension Method

相关文章:

  • 2021-08-02
  • 2022-01-01
  • 2022-12-23
  • 2021-10-12
  • 2021-12-23
  • 2022-12-23
  • 2021-10-31
猜你喜欢
  • 2021-06-18
  • 2021-12-16
  • 2022-02-02
  • 2022-01-11
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
相关资源
相似解决方案