0

I'm trying to use a contains in a list within a list but been stuck on this one:

var postFilter = PredicateBuilder.False<Company>();

// Loop through each word and see if it's in the company's facility
foreach (var term in splitSearch)
{
    var sTerm = term.Trim();
    postFilter = postFilter.Or(x => 
        x.Facilities.Contains(y=>
            y.Facility.Name.ToUpper().Contains(sTerm)) || 
        x => x.Facilities.Contains(y =>
            y.Facility.Description.ToUpper().Contains(sTerm)));
}

Postfilter is a list of companies, a company has a list of companyfacility items which is a 1:m relationship. A facility also has a 1:m relationship with this table. The x thus represents a companyfacility object. The y should represent a facility object.

(So a company can have many facilities, and a facility can belong to many companies. In between i use the companyfacility table for additional information of that companies facility - example, a specific company can have a lathe table that goes to diameter 300 where other companies would go to diameter 250, so it's important to have the table in between)

I want to return the companies which have the sTerm in their facility name or facility description, but this linq statement is invalid.

Thanks for the help!

2
  • I would avoid use of identifiers like x and y in nested lambdas. You can easily use company and facility as identifiers. Commented Mar 16, 2016 at 17:47
  • Yeah you're right, thanks for the pointer! Commented Mar 16, 2016 at 17:58

1 Answer 1

1

Here is the LINQ and some example code:

void Main()
{
    List<Company> postFilter = new List<UserQuery.Company>();

    var sTerm = "XXX".Trim().ToUpper();
    postFilter = postFilter.Where(x => 
                                 x.Facilities.Any(f => f.Name.ToUpper().Contains(sTerm)
                                                    || f.Description.ToUpper().Contains(sTerm))).ToList();
}

public class Company
{
    public string CompanyName { get; set; }
    public List<Facility> Facilities { get; set; }

}

public class Facility
{
    public string Name { get; set; }
    public string Description { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

using your input i found my solution, thanks a lot! postFilter = postFilter.Or(x => x.Facilities.Any(y => y.Facility.Name.ToUpper().Contains(sTerm)) || x.Facilities.Any(y => y.Facility.Description.ToUpper().Contains(sTerm)));

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.