So I have a list of existing salesmen in a branch in model.Salesmen as List<ApplicationUser>.
I want to generate a list of all other users to populate a dropdown menu for easy additions. What I've written so far, inspired by this SO post:
db.Users.Where(u => !model.Salesmen.Any(m => u.Id == m.Id)).OrderBy(u => u.Name).ToList();
The error I get:
Unable to create a constant value of type 'Leads.Models.ApplicationUser'. Only primitive types or enumeration types are supported in this context.
What am I doing wrong and how can I fix it?
You cannot put entire collection to lambda as EF cannot translate it to query. Do it like this:
var salesMenIds = model.Salesmen.Select(s => s.Id);
db.Users.Where(u => !salesMenIds.Contains(u.Id)).OrderBy(u => u.Name).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With