Search code examples
c#linqasp.net-mvc-5linq-to-entitieswhere-clause

Unable to use Contains in LINQ to Entity VS 2017 my query attached


my query is,

Here i am trying to get region id where the region name matches,

var Rid = db.Regions.Where(p => SqlFunctions.PatIndex("%" + Search + "%", p.RegionName) > 0).Select(p => p.RegionId).ToList();

Change Id's comma separated in string,

string joined = string.Join(",", Rid);

Trying to get all records related to Id's comma separated in string joined

var employ = db.Employees.Include(f => f.City).Include(f => f.Region).Where(p=>p.RegionId);

Issue is that we dont have contain when i tried use in where clause ??

Hopes for your suggestions


Solution

  • In Linq we have Any:

    var employ = db.Employees.Include(f => f.City)
                             .Include(f => f.Region)
                             .Where(p=> Rid.Any(rid => rid == p.RegionId));
    

    or :

    var employ = db.Employees.Include(f => f.City)
                             .Include(f => f.Region)
                             .Where(p=> Rid.Contains(p.RegionId));
    

    Now it will select those employees have the region id in the Rid list.