-1

I have a list of string

List<string> list = new List<string>();
list.Add("john");
list.Add("David");
list.Add("Sam");

Now I want to check whether my column in database contains these list Items.

var v = db.employee.where(s => s.Content.Contains(list));

My question is how can I match all list items to database column in just one query without using any loop. The query must return result if single list item is matched with column. The query I mentioned above not working. Please help me to solve this.

Babar
  • 109
  • 1
  • 2
  • 7
  • *" I want to check whether my column in database contains these list Items"*. Is that really what you want or it's `the list contains your column value`? – Ivan Stoev Mar 12 '16 at 10:05
  • Assuming your `s.Content` will have `john`, `David`, `sam`, or other things... I think you should do it the other way round... Nevertheless, it might be best if you could provide how `s.Content` look like... – Ian Mar 12 '16 at 10:06
  • @Ian I guess that's the case. Either way it will be a duplicate, I'm stepping out. – Ivan Stoev Mar 12 '16 at 10:12
  • 1
    Possible duplicate of [Linq to Entities - SQL "IN" clause](http://stackoverflow.com/questions/857973/linq-to-entities-sql-in-clause) – Ivan Stoev Mar 12 '16 at 10:17

7 Answers7

1

This will only work with the assumption that your db is an Entity Framework DbContext and that s.Content is a string. If you're using some other ORM then it may not work.

List<string> list = new List<string>();
list.Add("john");
list.Add("David");
list.Add("Sam");

var v = db.employee.Where(s => list.Contains(s.Content)).ToList();
Brad
  • 4,493
  • 2
  • 17
  • 24
0

You can do this:

List<string> list = new List<string>();
list.Add("john");
list.Add("David");
list.Add("Sam");
var v = db.employee
    .ToList()
    // This filtering doesn't happen in your SQL server.
    .Where(s => list.Any(x => s.Content.Contains(x)));

You can try to omit .ToList() line but I'm afraid your ORM will not know how to convert the query to SQL.

If you want the whole query to be executed in SQL you should consider building the query this way:

var query = db.employee.AsQueryable();
query = list.Aggregate(query, 
    (query, name) => query.Where(empl => empl.Content.Contains(name)));
Ivan Gritsenko
  • 4,166
  • 2
  • 20
  • 34
0

what about:

 List<string> list = new List<string> {"john", "Warwick", "Sam"};

 var vresult = _db.employee.Where(x => list.Contains(x.Content)).ToList();

please indicate what you are expecting...? like how is it not working?

Seabizkit
  • 2,417
  • 2
  • 15
  • 32
0

If return is different than null, exists.

public List<Employee> CheckIfExists(List<string> nameList)
{
    if (nameList == null)
    {
        return null;
    }

    string inClause = "";

    foreach (var item in nameList)
    {
        inClause = string.Format("{0},{1}", inClause, item);
    }

    return db.employee.SqlQuery("SELECT * FROM employee WHERE employee.Name IN (@p0)", inClause).ToList(); 
}
LP. Gonçalves
  • 454
  • 8
  • 26
0

Each element of list will be passed as an argument to s.Content.Contains and here s is an employee record.

var v = db.employee.Where(s => list.Any(s.Content.Contains));
M.S.
  • 4,283
  • 1
  • 19
  • 42
0

This might help you, list with name= {"john", "David", "Sam"} you want to check if John/David/Sam exists in the employee table. The below query is to check if the particular name exists in employee or not

var _data = _db.employee.Where(emp => list.Any(li => li.name == emp.name)).ToList();
0

LINQ in query syntax:

var result = from o in db.employee
             where list.Contains(o.Content)
             select o;
Meer
  • 678
  • 6
  • 12