Madhuri Lad
Madhuri Lad

Reputation: 241

In clause in lambda expression

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?

Upvotes: 12

Views: 41484

Answers (7)

ShaileshDev
ShaileshDev

Reputation: 1146

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.

Upvotes: 2

Chandan Rajbhar
Chandan Rajbhar

Reputation: 1

var memberData = (from u in _objGroupRepositoty.Value.GetUsers()
                                         join umedia in _objGroupRepositoty.Value.GetMediaDetails() on u.userid equals umedia.userid
                                         join gm in _objGroupRepositoty.Value.GetGroupMaster() on groupId equals gm.GDID
                                         join m in _objGroupRepositoty.Value.GetGroupMembers() on u.userid equals m.userid
                                         join media in _objGroupRepositoty.Value.GetMediaDetails() on gm.GDID equals media.GDID

                                         where (m.GDID == groupId
                                         && m.IsActive == true
                                         && gm.IsActive == true
                                         && u.IsActive == true)

                                         select new
                                         {
                                             userid = u.userid,
                                             firstname = u.firstname,
                                             lastname = u.lastname,
                                             mobile_no = u.mobile_no,
                                             imagepath = umedia.media_path,
                                             IsAdmin = m.IsAdmin,
                                             GroupID = gm.GroupID,
                                             group_name = gm.group_name,
                                             tagline = gm.tagline,
                                             groupImage = media.media_path,
                                             ChatRoomId = gm.ChatRoomId,
                                             OperationType

                                             //newMember = newMemberLists.Contains(u.userid) than "Y" : "N",
                                         }).ToList().Distinct();

Upvotes: -1

Tim Schmelter
Tim Schmelter

Reputation: 460238

The IN in LINQ is Enumerable.Contains or Enumerable.Any. Here are several approaches:

string[] strIDs = new[]{ "6", "7" };
int[] intIDs = new[]{ 1, 2 };
char[] charIds = new[]{ '4', '5' };

....
.Where(x => strIDs.Contains(x.Attribute("id"))
     || intIDs.Any(i => i.ToString() == x.Attribute("id"))
     || charIds.Any(c => c.ToString() == x.Attribute("id")));

Upvotes: 1

King King
King King

Reputation: 63377

You can do something like this:

var ids = new []{"id1","id2", ... };
var Charts = chartGroup.Descendants("charts")
                       .Elements("chart")
                .Where(x =>  ids.Contains(x.Attribute("id").Value))
                .Select(x => x.Attribute("name").Value).ToList();

Upvotes: 3

David Colwell
David Colwell

Reputation: 2590

If you have a set of values in an array, then you can use:

.Where(x => charids.Contains(x.Attribute("id").Value)

Upvotes: 26

Kirill Bestemyanov
Kirill Bestemyanov

Reputation: 11964

You can use Contains method of IEnumerable:

var ids = new[]{"1", "2"};
Where(x=>ids.Contains(x.Attribute("id").Value));

update:

moreover, this code will transfer in 'in' statement in SQL for IQueryable.

Upvotes: 3

Cătălin Rădoi
Cătălin Rădoi

Reputation: 1894

something like ... where charIds.Contains(x.Attribute"id".Value)

Upvotes: 0

Related Questions