predicate

Expression.Call - Calling linq extension: FirstOrDefault, Where

霸气de小男生 提交于 2019-12-01 17:23:51
I am trying to create the following dynamically, however I am having problems calling the extension method FirstOrDefault : using(var context = new Entities()) { var list = context.Engines.Include("Cars").Select(e => e.Cars.FirstOrDefault()).ToList(); } I have the following Expression parameter = Expression.Parameter(typeof(Engine), "e"); Expression property = Expression.Property(parameter, "Cars"); parameter = {e} property = {e.Cars} Those are good, but I am encountering a problem when I try and call the FirstOrDefault method: var result = Expression.Call(typeof(Queryable), "FirstOrDefault",

Why a `Predicate<T>` doesn't match a `Func<T,bool>`?

旧巷老猫 提交于 2019-12-01 15:08:22
I try to compile the following code in C#: public static T FirstEffective(IEnumerable<T> list) { Predicate<T> pred = x => x != null; return Enumerable.FirstOrDefault(list, pred); } The compiler (Mono/.NET 4.0) gives the following error: File.cs(139,47) The best overloaded method match for `System.Linq.Enumerable.FirstOrDefault<T>(this System.Collections.Generic.IEnumerable<T>,System.Func<T,bool>)' 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<T>'

Why a `Predicate<T>` doesn't match a `Func<T,bool>`?

不打扰是莪最后的温柔 提交于 2019-12-01 14:05:35
问题 I try to compile the following code in C#: public static T FirstEffective(IEnumerable<T> list) { Predicate<T> pred = x => x != null; return Enumerable.FirstOrDefault(list, pred); } The compiler (Mono/.NET 4.0) gives the following error: File.cs(139,47) The best overloaded method match for `System.Linq.Enumerable.FirstOrDefault<T>(this System.Collections.Generic.IEnumerable<T>,System.Func<T,bool>)' has some invalid arguments /usr/lib/mono/4.0/System.Core.dll (Location of the symbol related to

Default value on generic predicate as argument

拥有回忆 提交于 2019-12-01 13:46:15
问题 First time question for me :) I need some way to define a default predicate using a generic on the format Func<T, bool> and then use this as a default argument. Something like this: public bool Broadcast(byte command, MemoryStream data, bool async, Func<T, bool> predicate = (T t) => true) When i do this i get the compile error: Default parameter value for 'predicate' must be a compile-time constant Is there a smooth way of doing this that I am missing or should a make the predicate function

Combine two lambda expressions with inner expression

故事扮演 提交于 2019-12-01 12:05:55
I have the next class structure: 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<Authentication> Auths { get; set; } } I'm trying to build an expression at runtime to search entities by User.Name, Order.Name or User.Auths.Email There are three expressions I'm trying to combine: Expression<Func<Order, bool>> usernameExpression = order => order.Name.Contains(searchValue); Expression<Func<Order, bool>> nameExpression = order =>

Combine two lambda expressions with inner expression

无人久伴 提交于 2019-12-01 10:08:35
问题 I have the next class structure: 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<Authentication> Auths { get; set; } } I'm trying to build an expression at runtime to search entities by User.Name, Order.Name or User.Auths.Email There are three expressions I'm trying to combine: Expression<Func<Order, bool>> usernameExpression

Java - Use predicate without lambda expressions

倾然丶 夕夏残阳落幕 提交于 2019-12-01 09:13:12
I've below requirement:- Employee.java public boolean isAdult(Integer age) { if(age >= 18) { return true; } return false; } Predicate.java private Integer age; Predicate<Integer> isAdult; public PredicateAnotherClass(Integer age, Predicate<Integer> isAdult) { this.age = age; System.out.println("Test result is "+ isAdult(age)); } public void testPredicate() { System.out.println("Calling the method defined in the manager class"); } Now My goal is to test whether the age which i pass to Predicate is adult or not using the method defined in Employee class , for which i am passing the method

How to use predicate builder with linq2sql and OR operator

☆樱花仙子☆ 提交于 2019-12-01 08:58:05
I have two tables (TABLE1, TABLE2 - unique i know) that has a 1-to-many relationship respectively and a foreign key between ID columns of both tables. Using linq2sql I am trying to select all TABLE1 entries such that their corresponding TABLE2 values contains at least 1 item in the list I pass it. Here's some sample code I was using in LINQPad (awesome program) to test it out however am getting the error NotSupportedException: Unsupported overload used for query operator 'Any'. long[] items = { 3, 5, 8 }; var predicate = PredicateBuilder.False<TABLE2>(); foreach (long i in items) { long t = i;

How to use predicate builder with linq2sql and OR operator

感情迁移 提交于 2019-12-01 07:12:19
问题 I have two tables (TABLE1, TABLE2 - unique i know) that has a 1-to-many relationship respectively and a foreign key between ID columns of both tables. Using linq2sql I am trying to select all TABLE1 entries such that their corresponding TABLE2 values contains at least 1 item in the list I pass it. Here's some sample code I was using in LINQPad (awesome program) to test it out however am getting the error NotSupportedException: Unsupported overload used for query operator 'Any'. long[] items =

Is there a Java 1.5 equivalent to the Predicate<T> methods in .Net?

半世苍凉 提交于 2019-12-01 04:10:13
Specifically, I'm looking for similarly clean notation to the Collection<T>.TrueForAll / Exists , etc. It feels smelly to have to write a foreach loop to inspect the return of a method on each object, so I'm hoping there's a better Java idiom for it. Predicates are provided in the Google Collections library. Functional Java provides first-class functions. A predicate is expressed as F<T, Boolean> . For example, here's a program that tests an array for the existence of a string that is all lowercase letters. import fj.F; import fj.data.Array; import static fj.data.Array.array; import static fj