【问题标题】:Lambda expression to match in ICollection navigation property with an array在 ICollection 导航属性中与数组匹配的 Lambda 表达式
【发布时间】:2017-08-27 04:16:36
【问题描述】:

我有以下代码优先实体 -

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; }
}

我想根据字符串数组中的标签属性搜索联系人。类似于以下内容-

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

我无法弄清楚它是如何在 lambda 中完成的。有什么帮助吗?

【问题讨论】:

    标签: c# lambda icollection


    【解决方案1】:

    你可以试试这个

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

    编辑:

    获取匹配的联系人

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

    【讨论】:

    • 它选择列表。但我需要选择 List.
    猜你喜欢
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 2023-03-10
    • 2020-03-12
    • 2021-03-20
    • 2022-10-05
    • 1970-01-01
    相关资源
    最近更新 更多