c# - Query to filter contacts with given tags while having many to many relation between entities -


I am writing a simple contact management app. In my domain mode I have several relationships between contacts and tags. Now inside my MVC I need to ask users to pick selected tags and all the contacts have been returned, which will have any tag matched with the tag given by the user.

For example, if the contact selects "Tag" of Najma - "Author", "Blogger", "Customer" and "Customer" for search than "Author" and Najma, being in the result needed.

  Public class tag {public tag () {CreatedOn = DateTime.Now; } Public at tag id {received; Set; } Public string name {get; Set; } Created public date time {at; Set; } Public virtual iconenging & lt; Contacts & gt; Contact {get; Set; }} Public Relations {Public Contact () {isNewsletterSubscriber = true; Built-on = DateTime.Now; } Public int ContactID {get; Set; } Public string first name {receives; Set; } Public String LastName {get; Set; } Public String Email {get; Set; } Public string organization {get; Set; } Receive public string cellphone; Set; } Public string designation {get; Set; } Public string address {get; Set; } Receive the public string phone number { Set; } Public Bull Newsletter Subscriber {get; Set; } Public String Twitter {get; Set; } Public String Facebook {get; Set; } Created public date time {at; Set; } Public virtual icon & lt; Tag & gt; Tags {get; Set; }}  

I am playing with LinqPad linq expression but can not explain one solution for this. I tried to use inside and inside the ant where the section

Assume that you have the following contact : {Name = "blogger"} {new contact () {new name = "Najam", tag = new collection & lt; Tag & gt; }, New tag () {name = "customer"}}}, new contact () {first name = "mick", tag = new collection & lt; Tag & gt; () {New tag () {name = "author"}}}}, new contact () {first name = "ryan", tag = new collection & lt; Tag & gt; () {New tag () {name = "customer"}}}}};

and the following search tags:

  var tagsstose = new collections & lt; Tag & gt; () {New tag () {name = "author"}, new tag () {name = "blogger"}};  

You can query the following LINQ to search for a list of contacts with tags in the query:

  var searchResults = contacts.Where (c = Gt; c.Tags.Any (t = & gt; Tagstost. Any (tts = & gt; tts.Name == t.Name)));  

If you click on IEquatable & lt; Tag & gt; you can also do this:

  var searchResults = contacts.Where (c => c.Tags.Any (tagsToSearch.Contains));  

Comments