Question

I'm using LinqKit PredicateBuilder (http://www.albahari.com/nutshell/predicatebuilder.aspx) for a method that do searching. This is how the relationships are built (Entity Framework 4 CPT 5 POCO):

public class MusicSheet
{
    [Key]
    public int ID { get; set; }
    public string Title { get; set; }
    public string Key { get; set; }

    public virtual ICollection<Author> Authors { get; set; }
}

public class Author
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public string Bio { get; set; }

    public virtual ICollection<MusicSheet> MusicSheets { get; set; }
}

I need to be able to build a predicate that checks for MusicSheet (the Title contains a specific search term) as well as the Name or Bio of the Author who might also contain that search term. Here is what I currently have:

var predicate = PredicateBuilder.False<MusicSheet>();
foreach (var term in terms)
{
    string keyword = term;

    predicate = predicate
    .Or(s => s.Title.Contains(keyword));
    // TODO Check for Author Name & Bio
}

Any suggestions? Thank you very much.

Était-ce utile?

La solution

Have you tried this:

var predicate = PredicateBuilder.False<MusicSheet>();
foreach (var term in terms)
{
   string keyword = term;

   predicate = predicate
      .Or(s => s.Title.Contains(keyword) ||
               s.Authors.Any (a => a.Name.Contains(keyword) || a.Bio.Contains(keyword)));
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top