Search code examples
c#asp.netlinqlogic

LINQ expression to select based on visibility asp.net


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


Solution

  • Try this

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