Baba
Baba

Reputation: 2209

Filter data based on enum value

I have a model that looks like this

     [Column("StatusId")]
     public FileStatus Status { get; set; }

     public int Id { get; set; }

I also have an enum that looks like this.

     public enum FileStatus
        {
            MessageInProgress,
            MessageDone,
            NoMessage
        }

I am pulling data from my repository to populate data variable below

    IEnumerable<MyFile> data = await _fileDataRepository.GetAllData(Id);

I want to filter data such that I will remove any record that has a status of MessageDone or NoMessage.

This code does not work for me.

    data.where( x => x.Status != "MessageDone" | x.Status != "NoMessage")

How do I achieve the filtering?

Upvotes: 0

Views: 1037

Answers (1)

itaiy
itaiy

Reputation: 1284

2 things you need to change:

  1. FileStatus is enum and you use it as a string.
  2. You used | which is "boolean logical or" and you should use "Conditional logical and" && (You can use & but most of the times is better to use conditional operator because of the laziness (For more details)).

You should change it to:

data.where(x => x.Status != FileStatus.MessageDone && x.Status != FileStatus.NoMessage)

Or more simple:

data.where(x => x.Status == FileStatus.MessageInProgress)

Upvotes: 2

Related Questions