0

I have this request, and it works well. The main point of question is part where filterIds.Contains(tags.UserTagId). It looks for all tags with OR kind.So if I have filterIds with 1,2,3 value it selects contact that have any of this tag.

 var result = (from conts in _context.Contacts
            where conts.CreatorUserId == _userManager.GetUserId(this.User) &&
                  (from tags in _context.ContactTags
                      where filterIds.Contains(tags.UserTagId)
                      select tags.ContactId).Contains(conts.ID)
            select new
            {
                conts.ID,  conts.Name, });

I need to find all ContactId with AND kind. So if contact contains Ids 1, and 2 and 3 - so give it to me.It's like find all contacts that have ALL of this tags.

 from tags in _context.ContactTags
            where filterIds[0] == tags.UserTagId 
and filterIds[1] == tags.UserTagId 
and filterIds[n] == tags.UserTagId
            select ...

or even

 (from tags in _context.ContactTags
                where filterIds[0] == tags.UserTagId 

                select ...) where filterIds[1] == tags.UserTagId ...

How to do that?

user2377299
  • 93
  • 2
  • 8

1 Answers1

0

Find the solution with Count =) (modified to Framwork LINQ)

 var result = _context.Contacts.Where(conts => conts.CreatorUserId == _userManager.GetUserId(this.User)
                              && conts.Tags.Where(t => filterIds.Contains(t.UserTagId)).Count() == filterIds.Count)
                .Select(conts=> new...
user2377299
  • 93
  • 2
  • 8