Needed to replace int with int array

I have been struggling to update below query since yesterday but no luck.

In below query I needed to replace int idSiteno with int[] SelectedidSiteno - you see replacing int with int array.

I tried with .Contains .Any almost everything but no way near the solution. Could you please help here.

ICollection<Department> dbList = _db.Departments
                                 .Include(l => l.Sites)
                                 .Where(l => l.idCompany == idCompany && l.Disabled == "N" 
                                             && l.Sites.Any(x => x.idSite == idSiteno))
                                 .OrderBy(l => l.SortOrder).ToList();

Upvotes: 0

Views: 141

Answers (2)

NtFreX
NtFreX

Reputation: 11377

All the data from department table where company id matches and all idsite's of sites collection matches for all the selected id's in SelectedidSiteno

You can use All to check if every entry matches an specific expression. For example if all sites of an department match the selected collection.

ICollection<Department> dbList = _db.Departments
    .Include(l => l.Sites)
    .Where(l => l.idCompany == idCompany && 
        l.Disabled == "N" && 
        SelectedidSiteno.All(x => l.Sites.Any(s => s.idSite == x)))
    .OrderBy(l => l.SortOrder).ToList();

Upvotes: 0

Marcus H&#246;glund
Marcus H&#246;glund

Reputation: 16856

So the contains does not work?

ICollection<Department> dbList = _db.Departments
.Include(l => l.Sites)
.Where(l => l.idCompany == idCompany && l.Disabled == "N" && l.Sites.Any(x => SelectedidSiteno.Contains(x.idSite)))
.OrderBy(l => l.SortOrder).ToList();

Upvotes: 5

Related Questions