skip to Main Content

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

2

Answers


  1. 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 ||

    Login or Signup to reply.
  2. Try this

    var filteredList = list.Where(x => x.Visibility == Visibility.Public || (x.Visibility == Visibility.Private && x.AuthorId == currentUserId)).ToList();
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search