Próbuję kwerendy Posts
na podstawie listy Tags
:LINQ wiele-do-wielu skrzyżowaniu
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;}
}
Teraz chcę wrócić posty oparte listę tagów: 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
Kiedy post zawiera jeden lub więcej tagów, które istnieją również w searchTags
, powinny zostać uwzględnione w wyniku. Próbowałem następujących czynności:
var q = from s in Context.Registrations
where s.Tags.Intersect(tagList)
select s;
Błąd: 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.
Jakieś pomysły?
- aktualizacja 4 stycznia: Odpowiedzi wskazują na właściwe rozwiązanie, ale w moim kodzie wciąż mam wyjątek NotSupportedException. Czy jest możliwe, że liczba całkowita zerowalna powoduje to, ponieważ nie jest typem pierwotnym?
Próbowałem drugie rozwiązanie w kodzie LinqPad postu Marka Lindela i działa.Niestety, w moim kodzie mam ten sam UnsupportedException .. – Marthijn