Question

I have been looking at making a more generic WHERE clause so I don't to repeat code. I have the following that returns posts based on a tag. The same thing could used just as easily for return posts based on a search term or on a category. The only difference is the where clause. So from what i have seen expression trees or predicate builders could be used, though I don't know if these are the same. Also DLINQ was n option in an SO post from 2013. And LinqKit i guess is an option too. Can someone point in the right direction for creating a more generic where clause?

This is the part that I'd like to make dynamic Where(Function(t) t.PostTag.Any(Function(t1) t1.Tag.StartsWith(tag))) so that I could just as easily swpa in PostCategory or Posts in place of PostTag.

The code:

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

Was it helpful?

Solution

Solved -

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top