Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In clause in lambda expression

Tags:

c#

lambda

linq

var Charts = chartGroup
     .Descendants("charts")
     .Elements("chart")
     .Where(x => x.Attribute("id").Value == chartId.ToString())
     .Select(x => x.Attribute("name").Value).ToList();

Here I want to use an "in-clause"" (like the in clause in SQL) for Attribute("id").Value for array of strings:

like:

Where(x => x.Attribute("id").Value in ("1","2")
Where(x => x.Attribute("id").Value` in charIds[]

how to achieve this?

like image 455
Madhuri Lad Avatar asked Nov 26 '13 08:11

Madhuri Lad


1 Answers

SQL Where – In Functionality can be easily achieved by Lambda Expression. SQL query -

Select Name,Id from Category Where Id In (71,72)

Lambda Expression –

List checkedRecords = new List { 71, 72 };
var Cs = Context.Categories.Where(c => checkedRecords.Contains(c.Id));

I Hope this would help.

like image 98
ShaileshDev Avatar answered Oct 03 '22 07:10

ShaileshDev