linq-expressions

NSubstitute not matching Linq Expression

拥有回忆 提交于 2019-12-11 03:57:46
问题 I am implementing a repository pattern Query class and testing using NSubstitute. Repository interface: public interface IMyRepository { IQueryable<T> Query<T>(Expression<Func<T, bool>> filter) where T : class; } DateTimeProvider interface: public interface IMyDateTimeProvider { DateTime GetDateNow(); } Application interface: public interface IMyApplication { List<Thing> GetThingsByQuery(int status); } Application implementation: public class MyApplication : IMyApplication { private readonly

Changing the return type of an expression<func<>>

六眼飞鱼酱① 提交于 2019-12-11 02:24:10
问题 Say I have an Expression<Func<T,object>> is it possible to dynamically change the return type based on a Type variable to be something like Expression<Func<T,int>> I have the following class: public class ImportCheck<T> { public int id { get; set; } public string Name { get; set; } public Type Type { get; set; } public bool Required { get; set; } public int? MinLength { get; set; } public int? MaxLength { get; set; } public string Value { get; set; } public Expression<Func<T, object>>

Linq - Creating Expression<T1> from Expression<T2>

对着背影说爱祢 提交于 2019-12-10 14:29:42
问题 I have a predicate Expression<Func<T1, bool>> I need to use it as a predicate Expression<Func<T2, bool>> using the T1 property of T2 I was trying to think about several approches, probably using Expression.Invoke but couln;t get my head around it. For reference: class T2 { public T1 T1; } And Expression<Func<T1, bool>> ConvertPredicates(Expression<Func<T2, bool>> predicate) { //what to do here... } Thanks a lot in advance. 回答1: Try to find the solution with normal lambdas before you think

Trying to use parent property as parameter in child collection expression; LinqKit throws “Unable to cast MethodCallExpressionN to LambdaExpression”

喜夏-厌秋 提交于 2019-12-10 13:46:14
问题 I'm trying to dynamically construct an expression similar to the one below, where I can use the same comparison function, but where the values being compared can be passed in, since the value is passed from a property 'higher-up' in the query. var people = People .Where(p => p.Cars .Any(c => c.Colour == p.FavouriteColour)); I believe I've constructed the query correctly, but the ExpressionExpander.VisitMethodCall(..) method throws the following exception when I try to use it: "Unable to cast

Selecting Columns in LINQ using System.Linq.Expressions API

北战南征 提交于 2019-12-10 10:09:33
问题 I'm trying to use LINQ expressions to dynamically select columns from an IEnumerable into a result set that I can bind to my UI. At this point I am having a hard time just grasping the basics of projection in LINQ expressions. Let's say I have a list of strings like so: Dim myStrings = {"one", "two", "three"}.ToList() Using lambda expressions I can easily select out a collection of string lengths by doing: Dim myStringLengths = myStrings.Select(Function(x) x.Length) The result of this

Dynamic linq expression tree with nested properties

六眼飞鱼酱① 提交于 2019-12-10 09:39:37
问题 I have a list which I must filter on child properties. The filter operator is dynamic and I'm using a predicate builder in order to combine several filters/lambdas. For simplicity, let's say that I have two classes like this: public class FirstClass { public int Id { get; set; } public ICollection<SecondClass> MyList { get; set; } } public class SecondClass { public int ReferenceId { get; set; } public int Value { get; set; } } My filter use a reference id, an operator type and a value, such

Combine Multiple Linq Expressions [duplicate]

谁说胖子不能爱 提交于 2019-12-09 20:08:30
问题 This question already has answers here : Combining two expressions (Expression<Func<T, bool>>) (6 answers) Closed last year . I'm in the process of refactoring some code, attempting to make it more self-documenting. The current code has a query over an OData service which looks like this: return context.MessageLog.Where ( x => ( x.Status == MessageStatus.Success || x.Status == MessageStatus.Failure ) && x.Direction == MessageDirection.Inbound && x.ResponseDate == new DateTimeOffset(new

WhereNot linq expression

萝らか妹 提交于 2019-12-09 19:06:29
问题 I am trying to create an extension "WhereNot" So I can use: Dim x = "Hello world " Dim y = x.Split.WhereNot(AddressOf String.IsNullOrEmpty) Note that my aim here is to learn linq expressions; not solve my issue. I craated this function: <Extension()> _ Public Function WhereNot(Of TElement)(ByVal source As IQueryable(Of TElement), ByVal selector As Expression(Of Func(Of TElement, Boolean))) As IQueryable(Of TElement) Return source.Where(GetWhereNotExpression(selector)) End Function I don't

How to build a LambdaExpression from an existing LambdaExpression Without Compiling

試著忘記壹切 提交于 2019-12-09 12:57:45
问题 I want to combine two LambdaExpressions without compiling them. This is what it looks like if I do compile them: public Expression<Func<TContainer,bool>> CreatePredicate<TContainer,TMember>( Expression<Func<TContainer,TMember>> getMemberExpression, Expression<Func<TMember,bool>> memberPredicateExpression) { return x => memberPredicateExpression.Compile()(getMemberExpression.Compile()(x)); } That's obviously not the fastest way to get the target expression from the provided arguments. Also, it

LINQ Expression<Func<T, bool>> equavalent of .Contains()

时间秒杀一切 提交于 2019-12-09 11:07:43
问题 Has anybody got an idea of how to create a .Contains(string) function using Linq Expressions, or even create a predicate to accomplish this public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) { var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>()); return Expression.Lambda<Func<T, bool>> (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters); } Something simular to this would be ideal? 回答1: