我试图根据的Tags
名单上查询Posts
:LINQ许多一对多路口
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;}
}
现在我想回到立足岗位的标签列表: 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
当一个帖子包含一个或多个也存在于searchTags
中的标签,它应该包含在结果中。我曾尝试以下:
var q = from s in Context.Registrations
where s.Tags.Intersect(tagList)
select s;
错误: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;
运行时错误: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.
任何想法?
- 更新1月4日: 答案指向正确的解决方案,但在我的代码中,我仍然有NotSupportedException。它可能是可空的整数,因为它不是原始类型?
我已经尝试了Mark Lindel的帖子LinqPad代码中的第二个解决方案,它的工作原理。不幸的是,在我的代码中,我得到了相同的UnsupportedException .. – Marthijn 2012-01-04 07:50:15