linqkit

How to use an Expression<Func<Model, bool>> in a Linq to EF where condition?

别来无恙 提交于 2019-12-10 20:46:23
问题 There have already been some questions about this topic (for instance Expression.Invoke in Entity Framework?), however, I could not find an answer for my specific situation. I would like to define a method like this: public IQueryable<Customer> GetCustomers(Expression<Func<Customer, bool>> condition) { return from p in ctx.Customers.AsExpandable() where condition.Compile()(p) select p; } The AsExpandable method is from LinqKit (as it was adviced in the thread mentioned before). However, when

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

Building a custom predicate to act as a filter using a foreach loop

巧了我就是萌 提交于 2019-12-07 05:52:09
问题 I need to filter a list of documents by passing them to a custom filter that I'm struggling to build dynamically using a foreach loop : var mainPredicate = PredicateBuilder.True<Document>(); // mainPredicate is combined to other filters successfully here ... var innerPredicate = PredicateBuilder.False<Document>(); foreach (var period in periods) { var p = period; Expression<Func<Document, bool>> inPeriod = d => d.Date >= p.DateFrom && d.Date <= p.DateTo; innerPredicate = innerPredicate.Or(d =

Why am I getting an InvalidCastException when using an expression from a static field?

放肆的年华 提交于 2019-12-06 04:37:33
问题 I'm just starting to use LinqKit with EntityFramework 6.0.2 and I have the following question... Why does this: public static readonly Expression<Func<MyEnum, string>> ConvertToString = e => e == MyEnum.One ? "one" : e == MyEnum.Two ? "two" : "zero"; private static string GetSomethingElse(IQueryable<EnumTest> things) { var ret = things .AsExpandable() .Select(c => Program.ConvertToString.Invoke(c.SomeEnum)) .First(); return ret; } throw: An unhandled exception of type 'System

Adding to Lambda Expression and work with Entity Framework

你说的曾经没有我的故事 提交于 2019-12-05 13:26:58
If I want retrieve more columns with an already existing lambda tree expression like below, how would I do that? This works with Entity Frameworks and want it to still work. Expression<Func<DivisionTeam, DirectorTeamModel>> columns= (d) => new DirectorTeamModel { Id = d.Id, TeamId = d.Team.Id }; if (criteria.Template == ExportTemplate.Import || criteria.Template == ExportTemplate.Default) { // Retrieve additional columns from "columns" expression tree } return _divisionTeamsRepository.GetPagedResults(criteria.Page, criteria.PageSize, @where.Expand(), string.Format("{0} {1}", criteria.SortOrder

Building a custom predicate to act as a filter using a foreach loop

微笑、不失礼 提交于 2019-12-05 13:10:18
I need to filter a list of documents by passing them to a custom filter that I'm struggling to build dynamically using a foreach loop : var mainPredicate = PredicateBuilder.True<Document>(); // mainPredicate is combined to other filters successfully here ... var innerPredicate = PredicateBuilder.False<Document>(); foreach (var period in periods) { var p = period; Expression<Func<Document, bool>> inPeriod = d => d.Date >= p.DateFrom && d.Date <= p.DateTo; innerPredicate = innerPredicate.Or(d => inPeriod.Invoke(d)); } mainPredicate = mainPredicate.And(innerPredicate); This last line : documents

Why am I getting an InvalidCastException when using an expression from a static field?

别说谁变了你拦得住时间么 提交于 2019-12-04 10:10:23
I'm just starting to use LinqKit with EntityFramework 6.0.2 and I have the following question... Why does this: public static readonly Expression<Func<MyEnum, string>> ConvertToString = e => e == MyEnum.One ? "one" : e == MyEnum.Two ? "two" : "zero"; private static string GetSomethingElse(IQueryable<EnumTest> things) { var ret = things .AsExpandable() .Select(c => Program.ConvertToString.Invoke(c.SomeEnum)) .First(); return ret; } throw: An unhandled exception of type 'System.InvalidCastException' occurred in LinqKit.dll Additional information: Unable to cast object of type 'System.Linq

Use LinqKit PredicateBuilder for related model (EF Core)

孤街浪徒 提交于 2019-12-04 05:10:05
I want to use LinqKit's PredicateBuilder and pass the predicate into .Any method for related model. So I want to build a predicate: var castCondition = PredicateBuilder.New<CastInfo>(true); if (movies != null && movies.Length > 0) { castCondition = castCondition.And(c => movies.Contains(c.MovieId)); } if (roleType > 0) { castCondition = castCondition.And(c => c.RoleId == roleType); } And then use it to filter model that has relation to model in predicate: IQueryable<Name> result = _context.Name.AsExpandable().Where(n => n.CastInfo.Any(castCondition)); return await result.OrderBy(n => n.Name1)

Creating a dynamic Linq select clause from Expressions

馋奶兔 提交于 2019-12-03 16:26:51
Let's say I have defined the following variables: IQueryable<MyClass> myQueryable; Dictionary<string, Expression<Func<MyClass, bool>>> extraFields; // the dictionary is keyed by a field name Now, I want to tack on some dynamic fields to the IQueryable, so that it returns an IQueryable<ExtendedMyClass> , where ExtendedMyClass is defined as: class ExtendedMyClass { public MyClass MyObject {get; set;} public IEnumerable<StringAndBool> ExtraFieldValues {get; set;} } class StringAndBool { public string FieldName {get; set;} public bool IsTrue {get; set;} } In other words, for every value in

IQueryable<T> with EntityObject using Generics & Interfaces (Possible?)

天涯浪子 提交于 2019-12-01 12:04:39
I have a search repository for EntityFramework 4.0 using LinqKit with the following search function: public IQueryable<T> Search<T>(Expression<Func<T, bool>> predicate) where T : EntityObject { return _unitOfWork.ObjectSet<T>().AsExpandable().Where(predicate); } And another class which uses the IQueryable return value to subset the query in ways that are not possible using the Boolean LinqKit PredicateBuilder expressions: public IQueryable<T> SubsetByUser<T>(IQueryable<T> set, User user) where T : EntityObject { return set.Join(_searcher.Search<Metadatum>((o) => o.UserGUID == user.GUID), arc =