I am trying to query Posts based on the Tags list:
public class Post { public int? Id {get;set;} public string Name {get;set;} public virtual ICollection<Tag> Tags {get;set;} } public class Tag { public int? Id {get;set;} public string Name {get;set;} public vritual ICollection<Post> Posts {get;set;} }
Now I want to return messages based on the tag list: IList<Tag> searchTags = ParseTagsFromSearchString("tag1,tag2,tag3"); // this function checks the tags in the database, so all the primary keys are available in the list IList<Tag> searchTags = ParseTagsFromSearchString("tag1,tag2,tag3"); // this function checks the tags in the database, so all the primary keys are available in the list
If the message contains one or more tags that also exist in searchTags , it should be included in the result. I tried the following:
var q = from s in Context.Registrations where s.Tags.Intersect(tagList) select s;
Error: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<Models.Tag>' to 'bool'
var q = from s in Context.Registrations where s.Tags.Any(t => tagList.Any(t2 => t.Id.Value == t2.Id.Value)) select s;
Runtime Error: 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. 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. Any ideas?
- January 4th update: Answers indicate the correct solution, but in my code I still have a NotSupportedException. Is it possible that this value is nullable integer, since it is not a primitive type?
source share