Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Linq Lambda Left Join Where Right is Null [duplicate]

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?

like image 998
Ali Almohsen Avatar asked Aug 18 '26 08:08

Ali Almohsen


1 Answers

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();
like image 145
michal.jakubeczy Avatar answered Aug 19 '26 22:08

michal.jakubeczy