Tag: 谓词

为什么`Predicate `与`Func `不匹配?

我尝试在C#中编译以下代码: public static T FirstEffective(IEnumerable list) { Predicate pred = x => x != null; return Enumerable.FirstOrDefault(list, pred); } 编译器(Mono / .NET 4.0)给出以下错误: File.cs(139,47) The best overloaded method match for `System.Linq.Enumerable.FirstOrDefault(this System.Collections.Generic.IEnumerable,System.Func)’ has some invalid arguments /usr/lib/mono/4.0/System.Core.dll (Location of the symbol related to previous error) File.cs(139,47): error CS1503: Argument `#2′ cannot convert `System.Predicate’ expression to […]

将两个lambda表达式与内部表达式结合使用

我有下一个类结构: public class Order { public User User { get; set; } public string Name { get; set; } } public class Authentication { public string Email { get; set; } } public class User { public string Name { get; set; } public List Auths { get; set; } } 我正在尝试在运行时构建一个表达式,以按User.Name,Order.Name或User.Auths.Email搜索实体 我试图结合三种表达方式: Expression<Func> usernameExpression […]

如何使用linq2sql和OR运算符使用谓词构建器

我有两个表(TABLE1,TABLE2 – 我知道的唯一),它们分别具有1对多关系,并且两个表的ID列之间有一个外键。 使用linq2sql我试图选择所有TABLE1条目,使其相应的TABLE2值包含我传递它的列表中的至少1项。 这是我在LINQPad (真棒程序)中使用的一些示例代码来测试它但是我得到错误NotSupportedException:用于查询运算符’Any’的不支持的重载。 long[] items = { 3, 5, 8 }; var predicate = PredicateBuilder.False(); foreach (long i in items) { long t = i; predicate = predicate.Or(att => att.ID == t); } //TABLE2.Where(predicate).Dump(); //works like a charm IQueryable query = from t1 in TABLE1 where t1.TABLE2.AsQueryable().Any(predicate) //problem with this line select […]

LINQ to Entities查询中可重用的谓词表达式

在我们的应用程序中,许多不同查询中出现的一组标准已经慢慢变得更加复杂。 为了避免重复此代码,我想将这些条件拆分为一个方法,该方法将条件作为表达式返回,然后可以在必要时应用它: public Expression<Func> GetComplexPredicate() { // complex predicate is returned as an Expression: return c => … } 重复使用: var result = repository.Invoice.Where(GetComplexPredicate()) 但是,下面的语句不会编译,因为c.Invoice只是一个ICollection 。 var result = repository.Customer .Where(c => c.Country == “US” && c.Invoice.Any(GetComplexPredicate())) 是否可以使用这样的表达式?

C#,Linq2SQL:创建谓词以查找多个范围内的元素

假设我的数据库中有一个名为Stuff的东西,名为Id。 从用户那里我得到了一系列选定的Range对象(或者我是从输入中创建它们)和他们想要的ID。 该结构的精简版本如下所示: public struct Range : IEquatable<Range>, IEqualityComparer<Range> { public TA; public TB; public Range(T a, T b) { A = a; B = b; } … } 例如,人们可以得到: var selectedRange = new List<Range> { new Range(1, 4), new Range(7,11), }; 然后我想用它来创建一个谓词,只选择那些之间具有值的东西。 例如,使用PredicateBuilder ,我可以这样做: var predicate = PredicateBuilder.False(); foreach (Range r in selectedRange) { […]

MOQ – LINQ在设置方法中的谓词

在我的方法中,我有我的存储库这样做: bool isConditionMet = MyRepository.Any(x => x.Condition == true); 我试图使用MOQ来模拟这样: MyMockedRepository.Setup(x => x.Any(y => y.Condition == true)).Returns(true); 但是,当代码执行时,存储库调用始终返回false。 有没有办法用MOQ做到这一点? **编辑 – 每个请求添加代码** 我正在使用NHibernate所以我的Any方法在我的基础存储库中,并实现如下: public virtual bool Any(Expression<Func> predicate) { return Session.Query().Cacheable().Any(predicate); }

有没有办法否定谓词?

我想做这样的事情: List list1 = … List list2 = … Predicate condition = … … list2.RemoveAll (!condition); … list2.AddRange (list1.FindAll (condition)); 但是,这会导致编译器错误,如! 不能应用于Predicate 。 有没有办法做到这一点?

创建动态谓词 – 将属性作为参数传递给函数

我正在尝试创建动态谓词,以便它可以用于过滤列表 public class Feature { public string Color{get;set;} public string Weight{get;set;} } 我希望能够创建一个动态谓词,以便可以过滤List。 我得到一些条件,因为字符串值“>”,“ =”等。我有办法做到这一点吗? public Predicate GetFilter(X property,T value, string condition) //no clue what X will be { switch(condition) { case “>=”: return new Predicate(property >= value)//or something similar } } 用法可能是: var filterConditions=GetFilter(x=>x.Weight,100,”>=”); 如何定义GetFilter? 以及如何在其中创建谓词?

entity framework中的LINQ to Entities不支持LINQ表达式节点类型“Invoke”

任何人都可以帮助我解决我的问题。 我使用下面给出的代码: public IEnumerable Getdata(Expression<Func> predicate) { return AccountsContext.InvoiceHeaders.Include(“Company”).Include(“Currency”) .Include(“BusinessPartnerRoleList”).Include(“DocumentType”) .Where(predicate); } ….. 在我的代码中我使用如下 Expression<Func> predicate = PredicateBuilder.True(); predicate = predicate.And(o => o.CompanyId == oInvoiceHeader.CompanyId); List lstInvheader=Getdata(predicate).ToList(); 通过这样做,我得到了例外。 [System.NotSupportedException] — {“LINQ to Entities中不支持LINQ表达式节点类型’Invoke’。”}

如何在entity framework对象的LINQ to Entities中使用谓词

我在我的数据访问层中使用LINQ to Entities for Entity Framework对象。 我的目标是尽可能多地从数据库中过滤,而不将过滤逻辑应用于内存中的结果。 为此,业务逻辑层将谓词传递给数据访问层。 我的意思是 Func 所以,如果我直接使用这个谓词,就像 public IQueryable GetAllMatchedEntities(Func isMatched) { return qry = _Context.MyEntities.Where(x => isMatched(x)); } 我得到了例外 [System.NotSupportedException] — {“LINQ to Entities中不支持LINQ表达式节点类型’Invoke’。”} 该问题的解决方案建议使用LINQKit库中的AsExpandable()方法。 但是,再次使用 public IQueryable GetAllMatchedEntities(Func isMatched) { return qry = _Context.MyEntities.AsExpandable().Where(x => isMatched(x)); } 我得到了例外 无法将类型为“System.Linq.Expressions.FieldExpression”的对象强制转换为“System.Linq.Expressions.LambdaExpression” 是否有方法在LINQ to Entities查询entity framework对象中使用谓词,以便将其正确转换为SQL语句。 谢谢。