LinqKit PredicateBuilder与EF 4 CPT 5表关系?

时间:2011-02-23 20:47:35

标签: linq entity-framework-4 predicatebuilder

我正在使用LinqKit PredicateBuilder(http://www.albahari.com/nutshell/predicatebuilder.aspx)进行搜索。这就是关系的构建方式(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; }
}

我需要能够构建一个检查MusicSheet的谓词(Title包含特定搜索词)以及NameBio可能还包含该搜索词的作者。这是我现在拥有的:

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
}

有什么建议吗?非常感谢你。

1 个答案:

答案 0 :(得分:1)

你试过这个:

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