wiem
wiem

Reputation: 47

search filter with PredicateBuilder

I'm working in search filter we choose a student ( multiple checkbox )and get its exams and some exams can have two students for one student it works but when I choose multiple student I get exception The LINQ expression 'DbSet .Where(c => True && False || Invoke(r => r.listofStudents.Select(c => c.id).Contains(__broker_0), c[Exam]) && False || Invoke(r => r.listofStudents.Select(c => c.id).Contains(__student_0), c[Exam]) || Invoke(r => r.listofStudents.Select(c => c.id).Contains(__student_1), c[Exam]) )' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information. this is the code

var predicate = PredicateBuilder.True<Exam>();
  var predicate1 = PredicateBuilder.False<Exam>();
 if (students.Count < 2)
                {
                    predicate = predicate.And(r => r.listofStudents.Select(c => c.id).Contains(students.FirstOrDefault()));
                }
                else
                {
                        
                foreach (var student in students)
                {
                    predicate1 = predicate1.Or(r => r.listofStudents.Select(c => c.id).Contains(student ));
                    predicate = predicate.And(predicate1);
                }
                           
            }
 reqQuery = reqQuery.Where(predicate);

thank you

Upvotes: 0

Views: 429

Answers (1)

wiem
wiem

Reputation: 47

I found a solution to my problem

 var predicate = PredicateBuilder.True<Exam>();
  var predicate1 = PredicateBuilder.False<Exam>();
 if (students.Count < 2)
                {
                    predicate = predicate.And(r => r.listofStudents.Select(c => c.id).Contains(students.FirstOrDefault()));
                }
                else
                {
                        
                foreach (var student in students)
                {
                    predicate1 = predicate1.Or(r => r.listofStudents.Select(c => c.id).Contains(student ));
                }
                           
            }
 reqQuery = reqQuery.Where(predicate).Where(predicate1);

Upvotes: 0

Related Questions