Search code examples
c#linqpredicatebuilder

Set PredicateBuilder also on child collection


I am trying to apply the predicate not only to the entity parent but also to the child collection. The following is part of my code:

var predicate = PredicateBuilder.New<Entity>();
foreach (string keyword in keywords)
{
    string temp = keyword;
    predicate = predicate.Or(p => p.Name.Equals(temp));
    predicate = predicate.Or(p => p.Addresses.Select(x=> x.Name.Equals(temp));
}

The line predicate = predicate.Or(p => p.Addresses.Select(x=> x.Name.Equals(temp)); is not working ? any ideas as to why?

EDIT

In the following demo I am asking to get entity parent and child with name = tom but I also get child name = tom2.

private static void Main(string[] args)
{
    var result = GetAll(GetAll(), new List<string> { "tom" });
}

private static List<User> GetAll()
{
    return new List<User>
    {
        new User
        {
            Id = 1,
            Name = "tom",
            Addesses = new List<Addesses>
            {
                    new Addesses { Id = 1, Name = "tom" },
                    new Addesses { Id = 1, Name = "tom2" },
            }
        },
            new User
        {
            Id = 1,
            Name = "sam",
            Addesses = new List<Addesses>
            {
                    new Addesses { Id = 1, Name = "sam" },
                    new Addesses { Id = 1, Name = "sam2" },
            }
        },
    };
}

private static List<User> GetAll(List<User> users, List<string> keywords)
{
    var predicate = PredicateBuilder.New<User>();

    foreach (string keyword in keywords)
    {
        string temp = keyword;
        predicate = predicate.Or(p => p.Name.Equals(temp));
        predicate = predicate.Or(p => p.Addesses.Any(x => x.Name.Equals(temp)));
    }

    var result = users
        .Where(predicate)
        .ToList();

    return result;
}

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Addesses> Addesses { get; set; }
}

public class Addesses
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Solution

  • The call to p.Addresses.Select(x=> x.Name.Equals(temp) is not returning a boolean result.

    Depending on your actual logic you may want to look into Any:

    predicate = predicate.Or(p => p.Addresses.Any(x=> x.Name.Equals(temp));
    

    or All:

    predicate = predicate.Or(p => p.Addresses.All(x=> x.Name.Equals(temp));