Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify "not in" in this lambda expression?

I have a quick question becuase my brain won't work with me...
Where do I specify that I want the user_id's in 'Users' that are NOT in 'Groups' ?

db.Users.Join(db.Groups, a => a.user_id, b => b.user_id, (a, b) => new SelectListItem
{
  Value = a.user_id.ToString(),
  Text = a.surname + " " + a.lastname
});
like image 797
Niklas Avatar asked Jan 13 '12 13:01

Niklas


People also ask

What does => mean in lambda?

In lambda expressions, the lambda operator => separates the input parameters on the left side from the lambda body on the right side. The following example uses the LINQ feature with method syntax to demonstrate the usage of lambda expressions: C# Copy.

Can we write parameter less lambda expression?

No, there isn't. Lambda expressions are optimised (in terms of syntax) for the single parameter case. I know that the C# team feels your pain, and have tried to find an alternative.

How is this keyword handled inside a lambda expression?

The "this" and "super" references within a lambda expression are the same as in the enclosing context. Since the lambda expression doesn't define a new scope, "this" keyword within a lambda expression signifies "this" parameter of a method where the lambda expression is residing.

How do you use let in lambda expression?

There is no let , you just use a multi-line lambda and declare a variable: var results = Stores. Where(store => { var averagePrice = store. Sales.


1 Answers

The following should work (assuming that I understood your question correctly):

db.Users
.Where(x => !db.Groups.Any(y => y.user_id == x.user_id))
.Select(a => new SelectListItem
    {
        Value = a.user_id.ToString(),
        Text = a.surname + " " + a.lastname
    });
like image 193
Nuffin Avatar answered Oct 13 '22 09:10

Nuffin