我試圖根據的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