当前位置:网站首页>Everything is 2020, LINQ query you are still using expression tree
Everything is 2020, LINQ query you are still using expression tree
2020-11-07 20:57:00 【Irving the procedural ape】
1、 brief introduction
I'd like to recommend a good wheel to you today ,System.Linq.Dynamic.Core. We all know
Database applications often rely on “ dynamic SQL”, That is, queries constructed by program logic at run time . Splicing SQL Easy to cause SQL Inject , ordinary LINQ It can be done with an expression tree , But it's also troublesome . recommend System.Linq.Dynamic.Core It's more convenient to use .
This is a Microsoft Assembly .NET 4.0 Dynamic language function of .NET Core / Standard port .
Use this library , Can be in iQueryTable Write news on LINQ Inquire about ( Based on string ):
var query = db.Customers .Where("City == @0 and Orders.Count >= @1", "London", 10) .OrderBy("CompanyName") .Select("new(CompanyName as Name, Phone)");
2、 Use
2.1、nuget
Install-Package System.Linq.Dynamic.Core -Version 1.2.5
2.2、 Common way
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.........
版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢
边栏推荐
- 低代码 vs 模型驱动,它们之间到底是什么关系?
- ngnix集群高并发
- [original] the influence of arm platform memory and cache on the real-time performance of xenomai
- Exploration and practice of growingio responsive programming
- 【原创】ARM平台内存和cache对xenomai实时性的影响
- 洞察——风格注意力网络(SANet)在任意风格迁移中的应用
- C language I blog assignment 03
- Practice of Xiaoxiong school development board: real equipment access of smart street lamp sandbox experiment
- WPF 关于绘图个人总结
- Improvement of maintenance mode of laravel8 update
猜你喜欢
Improvement of maintenance mode of laravel8 update
30岁后,你还剩下什么?
Ubuntu下搜狗输入法的下载安装及配置
Big data algorithm - bloon filter
use Xunit.DependencyInjection Transformation test project
On the coverage technology and best practice of go code
三步一坑五步一雷,高速成长下的技术团队怎么带?
来自不同行业领域的50多个对象检测数据集
Insight -- the application of sanet in arbitrary style transfer
Three steps, one pit, five steps and one thunder, how to lead the technical team under the rapid growth?
随机推荐
Python 图片识别 OCR
工作1-3年的程序员,应该具备怎么样的技术能力?该如何提升?
14000 word distributed transaction principle analysis, master all of them, are you afraid of being asked in the interview?
Web安全(二)---跨域资源共享
Adobe Prelude / PL 2020 software installation package (with installation tutorial)
graph generation model
微服务的出现和意义的探索
Insight -- the application of sanet in arbitrary style transfer
How to learn technology efficiently
Cpp(二) 创建Cpp工程
Share several vs Code plug-ins I use everyday
What magic things can a line of Python code do?
来自不同行业领域的50多个对象检测数据集
建议患者自杀,OpenAI警告:GPT-3用于医疗目的风险太高
How did I lose control of the team?
屏读时代,我们患上了注意力缺失候群症
爆一个VS2015 Update1更新带来的编译BUG【已有解决方案】
Animation techniques and details you may not know
delphi10的rest.json与system.json的踩坑
Assembly function MCALL systemstack asmcgocal system call