linq

EF LINQ include nested entities [duplicate]

元气小坏坏 提交于 2021-02-11 12:03:28
问题 This question already has an answer here : Tree stucture table and entity framework (1 answer) Closed 3 years ago . I have multi-leveled entities with the following hierarchy: Parent-> RootChildren -> Children -> Children -> ..... Class Parent { public int Id {get; set;} public virtual Child RootChildren {get; set;} } Class Child { public virtual List<Child> Children {get; set;} } Now, I want to include entity which include all the child as nested way. I tried the following but it didn't work

C# 表达式树讲解(一)

泪湿孤枕 提交于 2021-02-11 11:14:35
一、前言 一直想写一篇Dpper的定制化扩展的文章,但是里面会设计到对Lambda表达式的解析,而解析Lambda表达式,就必须要知道表达式树的相关知识点。我希望能通过对各个模块的知识点或者运用能够多一点的讲解,能够帮助到园友了解得更多。虽然讲解得不全面,如果能成为打开这块的一把钥匙,也是蜗牛比较欣慰的。 表达式系列目录 C# 表达式树讲解(一) C# 表达式树遍历(二) C# 表达式树分页扩展(三) C# 表达式树Lambda扩展(四) 二、表达树理解 表达式树以树形数据结构表示代码,其中每一个节点都是一种表达式,它将我们原来可以直接由代码编写的逻辑以表达式的方式存储在树状的结构里,从而可以在运行时去解析这个树,然后执行,实现动态的编辑和执行代码。在.Net 里面的Linq to SQL就是对表达式树的解析。 这里先讲解下表达式和表达式树,表达式相信大家都知道,比如x+5或者5,都可以算是表达式,而表达式树里面的树指的二叉树,也就是表达式的集合,C#中的Expression类就是表达式类。对于一棵表达式树,其叶子节点都是参数或者常数,非叶子节点都是运算符或者控制符。 2.1、表达式的创建 Lambda表达式方法: Expression<Func< int , int , bool >> fun = (x, y) => x < y

表达式树(Expression Tree)

安稳与你 提交于 2021-02-11 10:41:32
表达式树是不可执行的代码,它只是用于表示一种树状的数据结构,树上的每一个节点都表示为某种表达式类型,大概有25种表达式类型,它们都派生自Expression类。创建表达式树具体有两个优势: 1.对表达式树的代码进行编辑修改,使表达式树中的代码变成动态代码,根据不同的数据库修改树上的代码逻辑从而达到动态切换数据库查询语句的目的,用表达式树可以动态构建针对不同数据库的查询语句。 2.完成类似反射访问未知对象的属性,通过动态构造表达式树,生成委托。 三种方式创建表达式树 Expression(表达式类) 此类可以称为表示表达式的类型,或也称为一棵表达式树。因为当你每创建一个表示表达式的实例时,都可以将该类型实例看成是一棵表达式树。每种表示表达式的类型都有一个具体的类型,如Expression的Variable()方法创建的是ParameterExpression类型的表达式,Expression的Add()方法创建的则是BinaryExpression类型的表达式。无论哪种表示表达式的类型都是从Expression派生。 //使用Expression的静态方法创建表达式 ParameterExpression variable = Expression.Variable ( typeof ( int ) , "x" ); LambdaExpression(Lambda表达式类)

C# 表达式树 Expression

谁说我不能喝 提交于 2021-02-11 10:18:27
表达式树是定义代码的数据结构。 它们基于编译器用于分析代码和生成已编译输出的相同结构。 几种常见的表达式 BinaryExpression 包含二元运算符的表达式 1 BinaryExpression binaryExpression = Expression.MakeBinary(ExpressionType.Add,Expression.Constant( 1 ),Expression.Constant( 2 )); 2 Console.WriteLine(binaryExpression.ToString()); // (1+2) 不进行溢出检查 3 binaryExpression = Expression.MakeBinary(ExpressionType.AddChecked, Expression.Constant( 3 ), Expression.Constant( 4 )); 4 Console.WriteLine(binaryExpression.ToString()); // (3+4) 进行溢出检查 5 binaryExpression = Expression.MakeBinary(ExpressionType.Subtract, Expression.Constant( 5 ), Expression.Constant( 6 )); 6 Console

Compare list of strings with object properties in another list

爷,独闯天下 提交于 2021-02-11 04:58:56
问题 I have a list of strings that contain course codes, and a list of objects who have courseCode as a property. I'm trying to find a linq expression to compare the two, and let me know if there are any matches, at all, between the items in the list of strings and the courseCode properties in the list of objects. I had a working expression moments ago and, long story short, I do not anymore and it's a miracle my laptop and monitors aren't in a million pieces :) Below is my current best guess at

Compare list of strings with object properties in another list

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-11 04:58:37
问题 I have a list of strings that contain course codes, and a list of objects who have courseCode as a property. I'm trying to find a linq expression to compare the two, and let me know if there are any matches, at all, between the items in the list of strings and the courseCode properties in the list of objects. I had a working expression moments ago and, long story short, I do not anymore and it's a miracle my laptop and monitors aren't in a million pieces :) Below is my current best guess at

async/await inside LINQ where clause not working

妖精的绣舞 提交于 2021-02-11 04:31:35
问题 I'm tring to make a database query inside a LINQ statement asynchronous, but I'm running into an error. The code below runs fine with out async/await var newEntities = _repositoryMapping.Mapper.Map<List<Entry>>(entries); newEntities = newEntities.Where(async e => await !_context.Entries.AnyAsync(c => c.Id == e.Id)).ToList(); Severity Code Description Project File Line Suppression State Error CS4010 Cannot convert async lambda expression to delegate type 'Func<Entry, bool>'. An async lambda

WPF通过KEPServerEX 读取PLC 的DB块值

╄→尐↘猪︶ㄣ 提交于 2021-02-11 01:43:16
按照自下而上的顺序 一.PLC 的 DB 块设置 PLC 为西门子的 S7 1200 PLC CUP 地址设置为 192.168.1.3 DB 块号为 22 。 PLC 内容的设置我不会,同事帮忙设置,此处内容省略,设置如下图 二、KEPServerEX 设置 KEPServerEX 为 6.4 简体中文版本 新建 点击新建弹出,如下图,点击是,更新 2 添加通道 名称为设置为 TEST ,网络设备器选择自己设置的本机 IP 地址: 192.168.1.50 以后步骤全为默认。 3. 添加设备 名称 Device PLC 选择 S1200 Ip 设置为 PLC 的 CPU IP 以后设置都为默认下一步到最后。 4.添加静态标记 名称为 Tag_01, 地址 为 DB22.X0.0 (之前 PLC 设置里面的第一个值,该值在 PLC 里面设置为间隔 1S 变化一次方便检测) 5. 点击 Quick Client 测试 KEPServerEX 通讯是否成功 如果 TEST.Device.Tag_01 对应的 Quality 值为 良好则为成功 三、C# 编写简单客户端程,类似 Quick Client 的检测功能 界面 (WPF) 前台代码 <Window x:Class="KEPClien.MainWindow" xmlns="http://schemas.microsoft.com

C# LINQ code for two list compare and replace

☆樱花仙子☆ 提交于 2021-02-10 23:52:37
问题 I have two list of object 'User', User u1 = new User { Id = 1, Name = "user1" }; User u2 = new User { Id = 2, Name = "user2" }; User u3 = new User { Id = 3, Name = "user3" }; User u3_1 = new User { Id = 10, Name = "user3" }; User u5 = new User { Id = 5, Name = "user5" }; User u6 = new User { Id = 6, Name = "user6" }; List<User> listOld = new List<User> { u1, u2, u3 }; List<User> listNew = new List<User> { u1, u2, u3_1, u5, u6 }; u3 and u3_1 is two objects which have same Name and different ID

C# LINQ code for two list compare and replace

风流意气都作罢 提交于 2021-02-10 23:48:42
问题 I have two list of object 'User', User u1 = new User { Id = 1, Name = "user1" }; User u2 = new User { Id = 2, Name = "user2" }; User u3 = new User { Id = 3, Name = "user3" }; User u3_1 = new User { Id = 10, Name = "user3" }; User u5 = new User { Id = 5, Name = "user5" }; User u6 = new User { Id = 6, Name = "user6" }; List<User> listOld = new List<User> { u1, u2, u3 }; List<User> listNew = new List<User> { u1, u2, u3_1, u5, u6 }; u3 and u3_1 is two objects which have same Name and different ID