Getting error "The parameter was not bound in the specified LINQ to Entities query expression."

c# entity-framework entity-framework-6 linq linq-to-entities

Question

I'm attempting to develop a general method for discovering deals whose dates fall within a range of dates that the user has selected. Below is my code:

var closeDates = new List<Range<DateTime>>
    {{ new Range<DateTime>{ Start = DateTime.Now, End = DateTime.Now.AddDays(1) }};

var deals = dbContext.Deals.AsQueryable();
deals = _helperService.ApplyDateRanges(deals, deal => deal.ClosedDate, closeDates);

A method called _helperService:

public IQueryable<T> ApplyDateRanges<T>(IQueryable<T> query, Expression<Func<T, DateTime>> dateSelector, IEnumerable<Range<DateTime>> dates)
{
    var enumerable = dates as Range<DateTime>[] ?? dates.ToArray();

    Expression<Func<T, Range<DateTime>, bool>> matcher =
        (row, rangeItem) => (rangeItem.Start == null || dateSelector.Invoke(row) >= rangeItem.Start) &&
                            (rangeItem.End == null || dateSelector.Invoke(row) < rangeItem.End.Value.AddDays(1));

    var expr = PredicateBuilder.False<T>();
    foreach (var dt in enumerable)
    {
        expr = expr.Or(d => matcher.Invoke(d, dt));
    }
    return query.AsExpandable().Where(expr.Expand());
}

But I'm experiencing a problem:The parameter 'deal' was not bound in the specified LINQ to Entities query expression. What is the root of this?

1
0
4/19/2016 3:17:17 PM

Accepted Answer

According to the exception notice, thedateSelector parameter has not been accurately rebounded. I'm unsure of the cause because a third party package is involved.

But I suppose this has something to do with your 12 zzz. I was going to offer you another straightforward manual expression construction-based working approach without the use of third-party software. And this is it (presumingRange class Start/End individuals are of kindDateTime? ):

public static class QueryableExtensions
{
    public static IQueryable<T> WhereIn<T>(IQueryable<T> source, Expression<Func<T, DateTime>> dateSelector, IEnumerable<Range<DateTime>> ranges)
    {
        var filter = ranges == null ? null : ranges.Select(range =>
        {
            var startFilter = range.Start != null ? Expression.GreaterThanOrEqual(dateSelector.Body, Expression.Constant(range.Start.Value.Date)) : null;
            var endFilter = range.End != null ? Expression.LessThan(dateSelector.Body, Expression.Constant(range.End.Value.Date.AddDays(1))) : null;
            return startFilter == null ? endFilter : endFilter == null ? startFilter : Expression.AndAlso(startFilter, endFilter);
        })
        .Where(item => item != null)
        .Aggregate(Expression.OrElse);
        return filter != null ? source.Where(Expression.Lambda<Func<T, bool>>(filter, dateSelector.Parameters[0])) : source;
    }
}

and you can use it easily like this (following the LINQ philosophy, no assistance service instance is required):

var deals = dbContext.Deals.WhereIn(deal => deal.ClosedDate, closeDates);
1
5/23/2017 11:52:40 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow