如何在Linq Where子句中包含变量?
这应该是一个简单的想法,我错过了,但已经尝试了一切来解决。。。。 基本上,我正在探索Linq和: 这项工作:如何在Linq Where子句中包含变量?,linq,where-clause,Linq,Where Clause,这应该是一个简单的想法,我错过了,但已经尝试了一切来解决。。。。 基本上,我正在探索Linq和: 这项工作: proj = Repo.Projects.Where(p => p.ID == 1 ).FirstOrDefault(); List proj=Repo.Projects.Where(p=>p.ID
proj = Repo.Projects.Where(p => p.ID == 1 ).FirstOrDefault();
List proj=Repo.Projects.Where(p=>p.ID<3.ToList()代码>
但这不起作用:
int i =3;
List<Project> proj = Repo.Projects.Where(p => p.ID < i ).ToList();
int i =1;
proj = Repo.Projects.Where(p => p.ID == i ).FirstOrDefault();
但这不起作用:
int i =3;
List<Project> proj = Repo.Projects.Where(p => p.ID < i ).ToList();
int i =1;
proj = Repo.Projects.Where(p => p.ID == i ).FirstOrDefault();
从DotNetFiddle获取以下消息:
Attempt by method 'DynamicClass.lambda_method(System.Runtime.CompilerServices.Closure, Project)' to access type 'Program+<>c__DisplayClass0' failed.
方法“DynamicClass.lambda_方法(System.Runtime.CompilerServices.Closure,Project)”尝试访问类型“Program+c_uDisplayClass0”失败。
我做错了什么?上下文是什么?这是Linq到对象、Linq到实体(实体框架)还是其他什么?请注意,不同Linq实现的功能不同。消息上说您正在使用动态,据我所知,它在LtO或LtE中没有使用,因此我需要更多信息。Hi-Dai,这是一个非常简单的程序,全部在40行dotnetfiddle测试中。仅对象:我创建了一个虚拟类项目,一个Repo类,它有一个仅包含4个项目实例的列表,以及一个将列表获取为可查询的方法。然后我测试了这些Linq表达式。使用制度;System.Collections.Generic;和系统。仅限Linq。完整程序位于: