LinqKit PredicateBuilder with EF 4 CPT 5 Relations in a Table?

I am using the LinqKit PredicateBuilder (http://www.albahari.com/nutshell/predicatebuilder.aspx) for the method that performs the search. Here's 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 create a predicate that checks MusicSheet (the Title contains a specific search term), as well as the Name or Bio author, which may also contain this search query, Here's what I have now:

 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.

+4
source share
1 answer

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))); } 
+1
source

Source: https://habr.com/ru/post/1341037/


All Articles