Jb_Dda
Jb_Dda

Reputation: 51

Get the Matched Property Name in linq where with multiple condition

i want to Implement FullText search on My Entity ... i have multiple Condition and now I want to know which of the conditions has been established ... In the final result, I will show the user what fields the search value was.

for Example keyword=Jack , was in FirstName Or LastName Or FatherName ?

my code like this :

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

Upvotes: 1

Views: 140

Answers (1)

jonaChaz
jonaChaz

Reputation: 301

You can create a custom class:

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

and

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();

this way you can know where the keywords was found.

Upvotes: 1

Related Questions