How to load Enum property into a Lambda

Asked

Viewed 42 times

0

I own property like that

public CustomerTypeRequest Type { get; set; }

That guy(CustomerTypeRequest) is an Enum, as below

public enum CustomerTypeRequest
    {
        Guest = 0, //Visitante anônimo
        Customer = 1, //Cliente final
        Reseller = 2, //Vendedor de uma revenda
    }

I need now in a Lambda bring all the ones that are 1 or Customer, like this

var qry = customer.Where(x => x.Type == 1);

What happens is that it gives error and I tried with equals too, no error, but it does not generate data. The error is this:

The operator '==' cannot be applied to type operands "Customertyperequest" and "int"

As a filter my list by this Enum type field?

  • 1

    Thus: var qry = customer.Where(x => x.Type == CustomerTypeRequest.Customer);

  • 1

    Answer that I mark

2 answers

2

var qry = customer.Where(x => x.Type == CustomerTypeRequest.Customer);

or

var qry = customer.Where(x => (int)x.Type == 1);

2


The way is to compare with the Enum CustomerTypeRequest which is the type that is configured in your Template:

var qry = customer.Where(x => x.Type == CustomerTypeRequest.Customer);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.