问题标签 [linq-expressions]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
linq - 测试 LINQ to SQL 表达式
我正在编写一个通过 LINQ to SQL 与 MS SQL 数据库一起工作的应用程序。我有时需要进行过滤,有时我的过滤条件太复杂,无法翻译成SQL查询。虽然我试图使它们可翻译,但我希望我的应用程序至少可以工作,尽管有时速度很慢。
LINQ to SQL 数据模型隐藏在存储库中,我不想为不同的情况提供多个 GetAll 方法重载,并注意在上层使用什么重载。所以我想测试我在存储库中的表达式是否可翻译,如果不是,则对整个数据集执行内存查询,而不是在查询实例化时抛出 NotSupportedException。
这就是我现在所拥有的:
searchExpression
是Expression<Func<TEntity, bool>>
如您所见,我正在FirstOrDefault
尝试实例化查询并在无法实例化时抛出异常。但是,当表达式良好时,它将执行无用的数据库调用。我可以使用Any
,Count
或其他方法,那样可能会更便宜一些FirstOrDefault
,但我想到的所有方法仍然需要花费昂贵的代价来访问数据库,而我所需要的只是测试我的表达方式。
在没有实际数据库调用的情况下,有没有其他方法可以说我的表达是“好”还是“坏”?
更新:
或者,更一般地说,有没有办法告诉 LINQ 在构造 SQL 失败时进行内存查询,从而根本不需要这种测试机制?
c# - 这个 linq 表达式创建有什么问题(使用包含)
我正在尝试使用 contains 运算符动态创建 linq 表达式。
在阅读了与该主题相关的几篇文章后,我提出了以下代码,可以让我执行“包含”之类的评估:
这就是这样调用的:
但是,在运行时,我收到如下错误:
“System.Func`2[DAL.BestAvailableFIP,System.Boolean]”类型的表达式不能用于返回类型“System.Boolean”
其中“DAL.BestAvailableFIP”是“TEntity”类型。
我确信这都与我对 lambda 表达式缺乏了解有关。谁能告诉我我做错了什么?
.net - 在 .NET 中构建简单的表达式树
我有一个界面,该用户指示它们之间的一些元素和运算符,我应该显示结果。
用户可以构建一个过滤器,比如p1 OP v1 OR p2 OP v2
wherep1
和p2
arePerson
属性Age, Name, Location
等,v1
并且v2
是比较值(10、'Maria'、'LA'),OP 是比较运算符(=、<、>),OR 是逻辑运算符(可以也是AND)。
例如:
Age > 18 AND Location = 'Paris'
,或另一个类似
Name Contains 'andro' AND Sex = 'm'
有了myPeople
集合和这个过滤器字符串,我如何使用 Linq.Expressions 构建和应用这个表达式?
我尝试使用DynamicLinq,但实际上我在使用 "Where" on 时遇到了问题List<Person>
,显然不是IQueryable
......
c# - 如何创建 NotStartsWith 表达式树
我jqGrid
用来向用户显示一些数据。jqGrid 具有搜索功能,可以进行字符串比较,如 Equals、NotEquals、Contains、StartsWith、NotStartsWith 等。
当我使用时,StartsWith
我得到了有效的结果(看起来像这样):
由于 DoesNotStartWith 不存在,我创建了它:
这行得通,我可以创建一个字符串并像这样调用这个方法:
所以现在我可以像这样创建/调用表达式:
但我得到一个ArgumentNullException was unhandled by user code: Value cannot be null.
Parameter name: method
错误。
有谁知道为什么这不起作用或更好的方法来解决这个问题?
c# - 使用 LINQ 的动态表达式。如何找到厨房?
我尝试实现一个用户动态过滤器,其中使用选择一些属性,选择一些运算符并选择值。
由于我还没有找到这个问题的答案,所以我尝试使用 LINQ 表达式。
主要是我需要确定所有主要房间是厨房的房子(任何感觉,我知道)。
c# - String.IndexOf 方法的表达式树
我应该如何构建表达式树string.IndexOf("substring", StringComparison.OrdinalIgnoreCase)
?
我可以在没有第二个参数的情况下让它工作:StringComparison.OrdinalIgnoreCase
. 到目前为止,这些是我的尝试:
也试过这个:
OrdinalIgnoreCase
请记住,如果我忽略该参数,我可以让它工作。
谢谢
c# - Linq - Expression.And 加入 N 个元素 BinayExpression
我有一个 BinaryExpression 列表,我需要将这个列表的元素加入到一个“对象”中。
在我的列表中,我有这个:
我需要这样的东西:
我知道 Linq 有 Expression.And,我需要制定一个逻辑来实现它,但我做不到。我试图在我的列表中创建一个循环,但 Expression.And 需要两个表达式,而在我的循环中我只有一个。在此之后,我将从这个“对象”创建一个 Expression.Lamba,如下所示:
nhibernate - Re-writing Linq Expression in NHibernate + DDD context
I have a thorny question about transforming Linq Expressions. I had a good search about, but I couldn't find anything that seems to cover this case. I'm reasonably familiar with Linq, at least in terms of creating and passing lambdas to methods, but I'm somewhat weaker on the Expression stuff.
First, some context: I have a generic persistence solution based on NHibernate used inside a number of DDD-ish projects. For special cases where a given collection of children within an aggregate could be essentially infinite (ie very large indeed), I cannot simply map a set or bag as it would never be acceptable for the entire collection to be loaded into memory. In this architecture it's not possible for code consuming the API to talk to the Repository directly to do a query or limit the results that way. I could of course not have a collection in the API at all and expose methods to retrieve relevant subsets of child objects instead (and if this doesn't work that's what I'll do), but I'm trying to do something slightly different and I've almost got it working...
These projects are being mapped with Fluent (not auto-mapping), and so I added a method to my base map class in the form
This method derives the relevant PropertyInfo
from the first Expression
(which specifies the member to map). The selector Expression
contains the relationship between the parent and child objects as a substitute for a normal NHibernate mapping.
So, suppose I have a selector in the map for a domain type User
(which is T
above):
This specifies a mapping between the subset of all Transactions
where Transaction.User
is the supplied User
u, and the User.Transactions
property which is IQueryable<Transaction>
. When an actual User
object is constructed, I need to turn this into
where this
is the User
object being constructed. In other words, I want to take a general rule that says how to map Users
to Transactions
and turn it into a rule about mapping this User
to Transactions
. I can then use this expression to generate an IQueryable<Transaction>
from the Repository by doing a Linq query, thus:
This can only work when the selector is Func<Transaction, bool>
, hence my ultimate need is to turn the original expression, which would generate a Func<User, Transaction, bool>
into Func<Transaction, bool>
.
This gives me an IQueryable
collection where all the query operations are being done as Linq-to-NHibernate queries and thus the entire collection never gets loaded into memory (yes, I know you could frame a query that would actually make it do that, but I can catch those at code review time).
Phew. Hope that makes sense.
Anyone with leet Expression re-writing skills able to point me in the right direction?
entity-framework-4.1 - 将多个包含语句传递到存储库中?
我试图找出一种方法将包含语句的集合传递到我的存储库中,以便我可以让它包含特定实体。以下是我的存储库中的一些示例代码。
GetByID 方法调用 GetSet 以返回实体集。我在想,如果我能以某种方式传入一组实体以包含(通过表达式)作为我的 GetById 的一部分,这样我就不必将 GetSet 公开给我的服务。所以,像这样:
var entity = _repository.GetById(theId, e => {e.Prop1, e.Prop2, e.Prop3});
然后我可以将该表达式传递给我的 GetSet 方法并将其传递给一个包含语句。想法?
linq - 帮助 nHibernate linq 提供程序扩展的表达式
我正在通过扩展 BaseHqlGeneratorForMethod 为 nHibernate 开发自定义 linq 扩展。该技术记录在这里: http: //fabiomaulo.blogspot.com/2010/07/nhibernate-linq-provider-extension.html
我已经成功地为各种类型的操作实现了这些,但我必须说 - 将一个简单的 linq 表达式转换为其完整的表达式树并不容易!我现在被困在一个上。
对于这个例子,我有三个实体。 Employee
, Group
, 和EmployeeGroup
. EmployeeGroup 类在 Employee 和 Group 之间建立了多对多的关系。我必须专门创建中间类,因为还有其他属性可以跟踪,例如每个员工在每个组中拥有的特定权限。所以有两个一对多的关系,而不是一个 nHibernate 的多对多关系。
现在假设我想获取包含特定员工的所有组。我可以写这个查询:
这很好用,但是要输入很多。我宁愿能够做到这一点:
我首先创建一个扩展方法,如下所示:
这在查询本地组列表时有效,但不适用于 nHibernate 会话。为此,我还必须创建一个 linq 扩展并注册它。就像在文章(上面链接)中一样,我创建了一个GroupHasEmployeeGenerator
扩展类BaseHqlGeneratorForMethod
。我将其.SupportedMethods
属性设置为引用我的 HasEmployee 扩展方法。
我迷路的地方是覆盖到BuildHql
. 要构建的表达式变得非常复杂。我想,因为我要替换该.Any
子句 - 一个好的起点是内置AnyHqlGenerator
类的源代码。但这并没有考虑到源是原始元素的属性,也没有考虑到我没有 lambda 表达式来表示 where 子句。我需要手动构建这些部分。
到目前为止,发布我的尝试毫无意义,因为它们都离任何可行的方法相去甚远。
有人可以帮我将这个简单的表达式转换为BuildHql
方法覆盖的适当方法集吗?
如果有任何更好的文档,请告诉我。谢谢。