Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ many-to-many intersection
    primarykey
    data
    text
    <p>I'm trying to query <code>Posts</code> based on a list of <code>Tags</code>:</p> <pre><code>public class Post { public int? Id {get;set;} public string Name {get;set;} public virtual ICollection&lt;Tag&gt; Tags {get;set;} } public class Tag { public int? Id {get;set;} public string Name {get;set;} public vritual ICollection&lt;Post&gt; Posts {get;set;} } </code></pre> <p>Now I want to return posts based a list of tags: <code>IList&lt;Tag&gt; searchTags = ParseTagsFromSearchString("tag1,tag2,tag3"); // this function checks the tags in the database, so all the primary keys are available in the list</code></p> <p>When a post contains one or more tags that also exists in <code>searchTags</code> it should be included in the result. I have tried the following:</p> <pre><code>var q = from s in Context.Registrations where s.Tags.Intersect(tagList) select s; </code></pre> <p>Error: <code>Cannot implicitly convert type 'System.Collections.Generic.IEnumerable&lt;Models.Tag&gt;' to 'bool'</code></p> <pre><code>var q = from s in Context.Registrations where s.Tags.Any(t =&gt; tagList.Any(t2 =&gt; t.Id.Value == t2.Id.Value)) select s; </code></pre> <p>Runtime error: <code>NotSupportedException: Unable to create a constant value of type 'Models.Tag'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.</code> Any ideas?</p> <p>-- update Jan. 4: The answers point to the right solution, but in my code I still have the NotSupportedException. Is it possible the nullable integer causes this since it is not a primitive type?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload