2017-08-27 2 views
0

je le code suivant d'abord entité -expression Lambda pour correspondre à la propriété de navigation ICollection avec un tableau

public class Contact 
{ 
    public Contact() 
    { 
     this.Tags = new HashSet<Tag>(); 
    } 

    public int ContactId { get; set; } 
    public string ContactName { get; set; }  
    public virtual ICollection<Tag> Tags { get; set; } 
} 

public class Tag 
{ 
    public Tag() 
    { 
     this.Contacts = new HashSet<Contact>(); 
    } 

    public int TagId { get; set; } 
    public string TagName { get; set; } 
    public virtual ICollection<Contact> Contacts { get; set; } 
} 

Je voudrais rechercher un contact basé sur la propriété Mots clés à partir d'un tableau de chaînes. Quelque chose comme le suivant-

//string[] tags 
Select from Db.Contacts where any Tag matched with any item in arrTags 

Je ne pouvais pas comprendre comment cela peut être fait en lambda. De l'aide?

Répondre

2

Vous pouvez essayer cette

var query = ctx.Contacts 
      .SelectMany(x => x.Tags) 
      .Where(z => YourTagArray.Contains(z.TagName); 

EDIT:

Pour obtenir les contacts correspondants

var query = ctx.Contacts.Where(x => x.Tags.Any(t => YourTagArray.Contains(t.TagName)); 
+0

Il sélectionne Liste . Mais je dois sélectionner la liste . –

+0

@ s.k.paul voir la réponse mise à jour. –