0

With following code, I get the error "LINQ to Entities does not recognize the method ..."

This is my linq query :

var retour = await base.Query().Where(f => f.HasbeenEnabled && f.UniqueID == campaingUniqueID)
            .Select(f => new GlobalInfoModel
            {
                FollowUpCompletion_Total = f.Questions
                        .SelectMany(g => g.Answers)
                        .FilterByKeyValue(filter)
                        .Count()
        }).FirstOrDefaultAsync();

and this is the static IQueryable method :

public static IQueryable<Answer> FilterByKeyValue(this IQueryable<Answer> query, KeyFilterModel[] filters)
    {

        var expendable = query.AsExpandable();

        if (filters != null && filters.Length > 0)
        {
            var queryand = PredicateBuilder.True<Answer>();
            foreach (var filter in filters)
            {
                var predicate = PredicateBuilder.False<Answer>();
                foreach (var value in filter.FilterValues)
                {
                    var key = string.Format(filter.Key);
                    var _value = string.Format(value);
                    predicate = predicate.Or(f => f.RespondentEntryID.HasValue && f.RespondentEntry.Values.Any(g => g.Key.Name.Equals(key) && g.Values.Equals(_value)));
                }
                queryand = queryand.And(predicate.Expand());
            }
            query = expendable.Where(queryand);
        }
        return query;
    }
  • possible duplicate of [LINQ to Entities does not recognize the method](http://stackoverflow.com/questions/7259567/linq-to-entities-does-not-recognize-the-method) – Aslam Jiffry Jul 13 '15 at 10:33
  • Try to use `AsEnumerable()` as `await base.Query().AsEnumerable().Where(...)...` I am not sure but it may solve this issue. – NASSER Jul 13 '15 at 10:53
  • 1
    @Aslam Jiffry in this question Joeri is using a library that supports the Or predicate concatenation. – bubi Jul 13 '15 at 11:09
  • If you decide to materialize the query with AsEnumerable or with ToList(so then everything can be evaluated) take care about real numbers of records in pruduction! – bubi Jul 13 '15 at 11:18

1 Answers1

0

First step, try to isolate the problem.

Anyway, probably is missing one Expand so EF does not recognize queryand.And. To concatenate the And predicates try to use Where (instead of And).

queryand = queryand.Where(predicate.Expand());
bubi
  • 6,414
  • 3
  • 28
  • 45
  • I don't think the FilterByKeyValue method is the problem, because when I set all the code in comment, I still get the same error. – Joeri Pansaerts Jul 13 '15 at 12:44
  • Did you comment it in var retour statement? Because is the only method that EF is trying to convert to SQL... – bubi Jul 13 '15 at 12:49