Krol Noir
Krol Noir

Reputation: 35

Selecting item from a list and converting to queue

How to do to select items that have certain criteria and soon after convert to queue?

public class Assinante
    {
        public int ID { get; set; }
        public string Nome { get; set; }
        public string Email { get; set; }
        public string CPF { get; set; }
        public string NossoNumero { get; set; }
        //public string Enviado { get; set; }
        public Status Pendente { get; set; }

        public Assinante(int id, string nome, string email, string cpf, string nossonumero, Status status)
        {
            this.ID = id;
            this.Nome = nome;
            this.Email = email;
            this.CPF = cpf;
            this.NossoNumero = nossonumero;
            this.Pendente = status;
        }
    }

public static Queue<Assinante> CreatQueueList()
{
    return new Queue<Assinante>(assinantes.Where(x => 
        x.Pendente != Status.Sent || 
        x.Pendente != Status.NotFound && 
        !string.IsNullOrEmpty(x.Email) &&
        !string.IsNullOrEmpty(x.NossoNumero)));
}

I have tried this way, however the return is still not as I wish ... it is returning objects that email is null

but I was looking for a more beautiful way to do this

Queue<Assinante> q = new Queue<Assinante>();
            foreach (var assinante in assinantes)
            {
                if (!string.IsNullOrEmpty(assinante.Email) && !string.IsNullOrEmpty(assinante.NossoNumero))
                {
                    q.Enqueue(assinante);
                }
            }

            return q;

Upvotes: 0

Views: 56

Answers (2)

Vinit
Vinit

Reputation: 2607

The use of logical operators seems to be incorrect, you can try this revised query.

return new Queue<Assinante>(assinantes.Where(x => 
        !(x.Pendente = Status.Sent 
        || x.Pendente = Status.NotFound 
        || string.IsNullOrEmpty(x.Email)
        || string.IsNullOrEmpty(x.NossoNumero))));

If you look at the where clause, if any one option is true then entire condition will be false and that particular item won't be added to queue.

Upvotes: 0

Sach
Sach

Reputation: 10393

This is almost certainly a case of logical operator precedence.

In C#, the Logical AND (&&) takes precedence over Logical OR (||).

This should help you understand:

bool result = true || true && false;   // --> true 
bool result = (true || true) && false; // --> false
bool result = true || (true && false); // --> true

And in your example, you have a statement that's similar to:

A || B && C && D

B && C && D will evaluate first, and then the result of that will || with A.

So going back to your original,

x.Pendente != Status.Sent || 
x.Pendente != Status.NotFound && !string.IsNullOrEmpty(x.Email) && !string.IsNullOrEmpty(x.NossoNumero))

Essentially, you're picking;

  • either objects whose Status != Sent,
    OR
  • objets whose Status != NotFound AND Email is not null or empty AND NossoNumero is not null or empty.

So, the first part allows object whose email is null to be selected in your Where clause.


EDIT

Also, note that if you group x.Pendente != Status.Sent || x.Pendente != Status.NotFound together, that might be a problem too. OR operation on two nots doesn't make a lot of sense.

Upvotes: 1

Related Questions