2010-03-29 4 views
8

J'ai un objet (produit) avec une propriété de type 'array'
product.tags = {"tag1", "tag2", "tag9"}La propriété LINQ-array contient un élément d'un autre tableau

J'ai un tableau d'étiquettes d'entrée à filtrer.

... mais ce n'est pas tout à fait travail:

List<string> filterTags = new List<string>() { "tag1", "tag3" }; 

var matches = from p in products 
    where p.Tags.Contains(filterTags) 
    select p; 

Toutes les recommandations? Merci.

Répondre

19

Que veut vraiment dire le Contains? Tous les articles au Tags doivent-ils exister dans filterTags? Ou au moins l'un d'entre eux? Pour ce dernier, utiliser Any et pour le premier utiliser All. Votre ligne where changerait à:

where p.Tags.Any(tag => filterTags.Contains(tag)) 

ou

where p.Tags.All(tag => filterTags.Contains(tag)) 
+0

Merci ... grand. Cela signifiait «tout» en réalité. .... "... afficher tous les produits contenant un ou plusieurs tags d'entrée". Je vais essayer. JE VOUS REMERCIE! – Rob

+0

Comme LINQ pour toute personne intéressée: 'context.Products.Where (p => p.Tags.Any (tag => filterTags.Contains (tag)))' –

Questions connexes