Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda expression "IN" operator Exists?

Tags:

I'm looking for to build the Lambda expression like the below

IQueryable<Object> queryEntity =                  _db.Projects.Where(Project=>Project.Id.IN(1,2,3,4)); 

I don't find any IN operator in Lambda expression.

Anybody have suggestions?

like image 781
sivaL Avatar asked Nov 22 '12 07:11

sivaL


People also ask

How do you use the operator in lambda expression?

In lambda expressions, the lambda operator => separates the input parameters on the left side from the lambda body on the right side. Func<string> greet = () => "Hello, World!"; Console. WriteLine(greet()); For more information, see Lambda expressions.

What are lambda operators?

The lambda operator => divides a lambda expression into two parts. The left side is the input parameter and the right side is the lambda body.

Why do we use lambda expression in C#?

Lambda expressions are like anonymous methods but with much more flexibility. When using a lambda expression, you don't need to specify the type of the input. Hence, a lambda expression provides a shorter and cleaner way of representing anonymous methods.

What is lambda expression in LINQ?

The term 'Lambda expression' has derived its name from 'lambda' calculus which in turn is a mathematical notation applied for defining functions. Lambda expressions as a LINQ equation's executable part translate logic in a way at run time so it can pass on to the data source conveniently.


1 Answers

Use IEnumerable.Contains for this.

var idList = new[] { 1, 2, 3, 4 }; IQueryable<Object> queryEntity =                  _db.Projects.Where(Project => idList.Contains(Project.Id)); 

You could construct the idList inline of course.

like image 80
Anders Arpi Avatar answered Dec 07 '22 22:12

Anders Arpi