0

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?

3
  • dont use strings. use && instead of |. don't capture as an IEnumerable as it will pull things too much. Commented Mar 4, 2020 at 17:19
  • Instead of using a string why not just use the enum itself? x.Status != FileStatus.MessageDone. Also you should have && instead of | for the comparison operator. Commented Mar 4, 2020 at 17:20
  • I'd add an overload GetAllData(Id, status) where status is one of FileStatus values and do the filter in the DAL. Commented Mar 4, 2020 at 18:04

1 Answer 1

2

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)

Sign up to request clarification or add additional context in comments.

5 Comments

Good answer and explanation with solution; the last code snippet is best.
The reason why I used Or | is that I want to filter the data when the enum is MessageDone, or NoMessage. Both of this message cannot exist at the same time. It is just one at a time that can exist in the table
from MSDN: The result of x | y is true if either x or y evaluates to true. As you mentioned, In your case x.Status can't be both, so one of the operands will be always true.
The filtering is not working. I am still getting a record back that has a status of MessageDone and it should not come back
Maybe it changes after the filter? You can put a breakpoint on the setter of Status and check it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.