如何制作更通用的WHERE子句?

时间:2014-07-30 13:55:23

标签: vb.net predicate entity-framework-6.1 expressionbuilder

我一直在考虑制定更通用的WHERE条款,因此我不会重复代码。我有以下内容根据标记返回帖子。对于基于搜索词或类别的回复帖子,同样可以使用同样的东西。唯一的区别是where子句。因此,从我所看到的表达树或谓词构建器可以使用,虽然我不知道这些是否相同。此外,DLINQ在2013年的SO帖子中是n选项。而LinqKit我猜也是一个选项。有人能指出正确的方向来创建更通用的where子句吗?

这是我想制作动态Where(Function(t) t.PostTag.Any(Function(t1) t1.Tag.StartsWith(tag)))的部分,这样我就可以轻松地在PostCategory或Posts中代替PostTag。

代码:

Return _postRepository.SelectAll.Where(Function(t) t.PostTag.Any(Function(t1) t1.Tag.StartsWith(tag))).Select(Function(S) New be_PostsViewModel With {.IsPublished = S.PostIsPublished, .Id = S.PostId, .PostSummary = S.PostSummary, .PostDateCreated = S.PostDateCreated, .PostTitle = S.PostTitle, .PostTags = S.PostTag}).OrderByDescending(Function(d) d.PostDateCreated).Where(Function(p) p.IsPublished = True).Skip((Page - 1) * PageSize).Take(PageSize).ToList

1 个答案:

答案 0 :(得分:0)

解决了 -

Dim PostsByTagExpression As Expression(Of Func(Of PostSummaryDTO, Boolean)) =
Function(p) p.PostTag.Any(Function(t1) t1.Tag.StartsWith(Word))

Dim postsbytag = _postRepository.SelectAll.AsExpandable.Where(PostsByTagExpression)
.Select