Problem searching (Firstordefault) - Linq

Asked

Viewed 94 times

0

I’m having a problem conducting the following search:

var estDel = bdprincipalEntities.unidades
            .FirstOrDefault(x => x.uni_codigo == uni_codigo && x.Emp_codigo == Emp_codigo);

the field is of the string type:

public string uni_codigo { get; set; }

My problem is this: when I do a search for a field that receives value "20", or starting with any numeric value, the search returns null, but if I search with "metro" right, I don’t understand what it could be, being that "20" is considered string also

Registration exists in the database:

inserir a descrição da imagem aqui

  • 1

    When you say "start with any numerical value" do you want to do something like the "like" of SQL? Because in your select will return only values that match exactly the values you specified for both uni_code and Code Emp_code.

  • 2

    That would be my question, do you want an exact search or what contain the value?

  • Sorry for the delay, but I want you to return an exact search, the code has to be exactly 20

  • The record exists in the database, I will post an image

  • 1

    The Emp_code in the database is null, out of curiosity, what value you are passing in your Emp_code?

  • Ah, you’re right, I didn’t touch that, I pass default value that is 1, that’s the error, post an answer that I accept

Show 1 more comment

1 answer

4


As explained in the comments of the question, the problem is not the LINQ and yes in the search made, because in the line of your bank, as the image, the column "Emp_codigo" is null, but is being passed the same value.

In this way, use the operator OR for this query, nullify the value or omit the field of your search, depending on your business rule.

Browser other questions tagged

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