0

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();
3
  • what do you want to query? Commented Jun 22, 2017 at 18:21
  • Do you want to use a query similar to the Where Id IN (select ... of SQL? Commented Jun 22, 2017 at 18:24
  • My query will be to extract 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 Commented Jun 22, 2017 at 18:27

2 Answers 2

5

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();
Sign up to request clarification or add additional context in comments.

5 Comments

@GagandeepSinghRandhawa please upvote/mark as answer if this answer helped you
@MarcusH Right answer +1. However it seems the OP is not familiar with voting and accepting, based on his profile and his other questions unfortunately.
@S.Akbari you are a true hero! To bad there are so many who just take answers and don't have the mannors to accept
Sorry to spoil the party, but I don't think this is what OP asked for. I think they want Departments with only Sites that match a couple of ids. Yet another incarnation of the filtered Include question...
@GertArnold when I read the OPs first comment on my answer I think he was satisfied..but who knows
0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.