在具有多个条件的linq中获取匹配的属性名称

时间:2019-07-13 14:40:41

标签: c# entity-framework linq entity-framework-core full-text-search

我想对“我的实体”实施全文搜索...我有多个条件,现在我想知道已经建立了哪些条件...在最终结果中,我将向用户显示搜索值的字段是。

例如example = Jack,位于FirstName或LastName或FatherName中?

我的代码是这样的:

Users.Where(x => x.FullName.Contains(keywords) || x.LastName.Contains(keywords) || x.FatherName.Contains(keywords)).ToList();

1 个答案:

答案 0 :(得分:1)

您可以创建一个自定义类:

public class SearchUser
{
    public string FullName { get; set; }
    public string LastName { get; set; }
    public string FatherName { get; set; }

    public bool searchFullName { get; set; }
    public bool searchLastName { get; set; }
    public bool searchFatherName { get; set; }
}

Users.Where(x => x.FullName.Contains(keywords) ||
        x.LastName.Contains(keywords) ||
        x.FatherName.Contains(keywords))
        .Select(x => new SearchUser()
        {
            FatherName = x.FatherName,
            FullName = x.FullName,
            LastName = x.LastName,

            searchFatherName = x.FatherName.Contains(keywords),
            searchFullName = x.FullName.Contains(keywords),
            searchLastName = x.LastName.Contains(keywords)
        }).ToList();

这样您就可以知道在哪里找到关键字。