Home > Back-end >  LINQ expression to select based on visibility asp.net
LINQ expression to select based on visibility asp.net

Time:04-10

So I have a list with post objects that have a visibility enum property. I'm trying to get all posts that have visibility == visibility.public and visibility == visibility.private && AuthorId == currentUserId.

This doesn't seem to work: from e in list select (e.Visibility == Visibility.Public && (e.Visibility == Visibility.Private && e.Author.Id == userId))

Any help would be appreciated

CodePudding user response:

You are asking that a post has visibility public AND private at the same time. Your two conditions should use an OR not an AND.

Your condition should be e.Visibility == Visibility.Public || (e.Visibility == Visibility.Private && e.Author.Id == userId)

Notice the ||

CodePudding user response:

Try this

var filteredList = list.Where(x => x.Visibility == Visibility.Public || (x.Visibility == Visibility.Private && x.AuthorId == currentUserId)).ToList();

  • Related