casillas
casillas

Reputation: 16813

Passing a list<string> into Linq

The first query returns a list of string, and I am passing them into another table to find corresponding items, but nothing happens. no error message or nothing

var classIds = _contextSpecRepo.Get(x => x.cId.Equals(cId)).Select(x => x.classNames).Distinct().ToList();
// issue happens in the following query
var classes= Repository.Get(x => x.Id.Equals(classIds)).ToList();

Upvotes: 2

Views: 790

Answers (3)

sebu
sebu

Reputation: 2954

you can also skip .ToList()

var classes= Repository.Get(x => classIds.Contains(x.Id));

Upvotes: 1

Igor
Igor

Reputation: 62238

If you must do it in 2 queries then you have to use contains

var classes= Repository.Get(x => classIds.Contains(x.Id)).ToList();

A better solution would be to use a join on the tables.

Upvotes: 3

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

The call to Equals, which takes object, hides the problem: you are comparing a single Id to a list of Ids, rather than checking if the Id is present in a collection. This compiles, but yields no result.

Here is how you can fix it:

var classes= Repository.Get(x => classIds.Any(y => y == x.Id)).ToList();

or

var classes= Repository.Get(x => classIds.Contains(x.Id)).ToList();

Upvotes: 5

Related Questions