当前位置:网站首页>什么都2020了,LINQ查询你还在用表达式树
什么都2020了,LINQ查询你还在用表达式树
2020-11-07 20:57:00 【程序猿欧文】
1、简介
今天给大家推荐个好的轮子,System.Linq.Dynamic.Core。我们都知道
数据库应用程序经常依赖于“动态SQL”,即在运行时通过程序逻辑构造的查询。拼接SQL容易造成SQL注入,普通的LINQ可以用表达式树来完成,但也比较麻烦。推荐System.Linq.Dynamic.Core用起来比较方便。
这是Microsoft程序集的.NET 4.0动态语言功能的.NET Core /标准端口。

使用此库,可以在iQueryTable上编写动态LINQ查询(基于字符串):
var query = db.Customers .Where("City == @0 and Orders.Count >= @1", "London", 10) .OrderBy("CompanyName") .Select("new(CompanyName as Name, Phone)");
2、使用
2.1、nuget
Install-Package System.Linq.Dynamic.Core -Version 1.2.5
2.2、常见方式
using System.Collections;using System.Collections.Generic;using System.Linq.Dynamic.Core.Tests.Helpers;using System.Linq.Dynamic.Core.Tests.Helpers.Models;using System.Linq.Expressions;using System.Reflection;namespace System.Linq.Dynamic.Core.ConsoleTestApp{ public static class E { public static IQueryable GroupBy2<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keyLambda2) { //LambdaExpression keyLambda = DynamicExpression.ParseLambda(source.ElementType, null, "new (Profile.Age)", null); LambdaExpression x = (LambdaExpression)keyLambda2; //return source.Provider.CreateQuery<IGrouping<TKey, TSource>>( // Expression.Call( // typeof(Queryable), "GroupBy", // new Type[] { source.ElementType, keySelector.Body.Type }, // new Expression[] { source.Expression, Expression.Quote(keySelector) } // )); return source.Provider.CreateQuery( Expression.Call( typeof(Queryable), "GroupBy", new Type[] { source.ElementType, x.Body.Type }, new Expression[] { source.Expression, Expression.Quote(x) })); } } public class Program{ public static void Main(string[] args) { Console.WriteLine("--start"); DynamicProperty[] props = { new DynamicProperty("Name", typeof(string)), new DynamicProperty("Birthday", typeof(DateTime)) }; Type type = DynamicClassFactory.CreateType(props); DynamicProperty[] props2 = { new DynamicProperty("Name", typeof(string)), new DynamicProperty("Birthday", typeof(DateTime)) }; Type type2 = DynamicClassFactory.CreateType(props2); DynamicProperty[] props3 = { new DynamicProperty("Name", typeof(int)), new DynamicProperty("Birthday", typeof(DateTime)) }; Type type3 = DynamicClassFactory.CreateType(props3); DynamicClass dynamicClass = Activator.CreateInstance(type) as DynamicClass; dynamicClass.SetDynamicPropertyValue("Name", "Albert"); dynamicClass.SetDynamicPropertyValue("Birthday", new DateTime(1879, 3, 14)); Console.WriteLine(dynamicClass); string n1 = dynamicClass["Name"] as string; Console.WriteLine("dynamicClass[\"Name\"] = '" + n1 + "'"); dynamicClass["NameX"] = "x"; string n2 = dynamicClass["NameX"] as string; Console.WriteLine("dynamicClass[\"NameX\"] = '" + n2 + "'"); //GroupByAndSelect_TestDynamicSelectMember(); //Select(); //TestDyn(); //ExpressionTests_Enum(); //Where(); //ExpressionTests_Sum(); Console.WriteLine("--end"); } private static void GroupByAndSelect_TestDynamicSelectMember() { var testList = User.GenerateSampleModels(51).Where(u => u.Profile.Age < 23); var qry = testList.AsQueryable(); var rrrr = qry.GroupBy2(x => new { x.Profile.Age }); var ll = rrrr.ToDynamicList(); var byAgeReturnAllReal = qry.GroupBy(x => new { x.Profile.Age }).ToList(); var r1 = byAgeReturnAllReal[0]; //var byAgeReturnOK = qry.GroupBy("Profile.Age").ToDynamicList(); // - [0] {System.Linq.Grouping<<>f__AnonymousType0<int?>, System.Linq.Dynamic.Core.Tests.Helpers.Models.User>} object {System.Linq.Grouping<<>f__AnonymousType0<int?>, System.Linq.Dynamic.Core.Tests.Helpers.Models.User>} var byAgeReturnAll = qry.GroupBy("new (Profile.Age)").OrderBy("Key.Age").ToDynamicList(); var q1 = byAgeReturnAll[0]; var k = q1.Key; int? age = k.Age; foreach (var x in byAgeReturnAllReal.OrderBy(a => a.Key.Age)) { Console.WriteLine($"age={x.Key.Age} : users={x.ToList().Count}"); } foreach (var x in byAgeReturnAll) { Console.WriteLine($"age={x.Key.Age} : users={x}"); } } private static void TestDyn() { var user = new User { UserName = "x" }; dynamic userD = user; string username = userD.UserName; Console.WriteLine("..." + username); } public static void ExpressionTests_Enum() { //Arrange var lst = new List<TestEnum> { TestEnum.Var1, TestEnum.Var2, TestEnum.Var3, TestEnum.Var4, TestEnum.Var5, TestEnum.Var6 }; var qry = lst.AsQueryable(); //Act var result1 = qry.Where("it < Te.........
版权声明
本文为[程序猿欧文]所创,转载请带上原文链接,感谢
https://my.oschina.net/mikeowen/blog/4707705
边栏推荐
- The official 1909 version of win10 cannot open the real-time protection solution of virus and threat protection in windows security center.
- ajax 载入html后不能执行其中的js解决方法
- DOM节点操作
- 工作1-3年的程序员,应该具备怎么样的技术能力?该如何提升?
- When tidb and Flink are combined: efficient and easy to use real-time data warehouse
- [original] the influence of arm platform memory and cache on the real-time performance of xenomai
- 当 TiDB 与 Flink 相结合:高效、易用的实时数仓
- 你可能不知道的Animation动画技巧与细节
- 如何以计算机的方式去思考
- DOM节点操作
猜你喜欢

Deep into web workers (1)

HandlerMethodArgumentResolver使用和原理

华为HCIA笔记

Insight -- the application of sanet in arbitrary style transfer

Annual salary of 900000 programmers is not as good as 3800 civil servants a month? How to choose between stability and high income?

微服务的出现和意义的探索

Web API series (3) unified exception handling

从技术谈到管理,把系统优化的技术用到企业管理

浅谈HiZ-buffer

工作1-3年的程序员,应该具备怎么样的技术能力?该如何提升?
随机推荐
How to choose a good company
年薪90万程序员不如月入3800公务员?安稳与高收入,到底如何选择?
[original] the influence of arm platform memory and cache on the real-time performance of xenomai
How did I lose control of the team?
MongoDB下,启动服务时,出现“服务没有响应控制功能”解决方法
一万四千字分布式事务原理解析,全部掌握你还怕面试被问?
go wire 依赖注入入门
工作1-3年的程序员,应该具备怎么样的技术能力?该如何提升?
Business Facade 与 Business Rule
The official 1909 version of win10 cannot open the real-time protection solution of virus and threat protection in windows security center.
sed之查找替换
如何应对事关业务生死的数据泄露和删改?
awk实现类sql的join操作
如何以计算机的方式去思考
不要把异常当做业务逻辑,这性能可能你无法承受
一文详解微服务架构
Didi's distributed ID generator (tinyid), easy to use
Git代码提交操作,以及git push提示failed to push some refs'XXX'
Analysis of kubernetes service types: from concept to practice
use Xunit.DependencyInjection Transformation test project