简体   繁体   中英

How to compare linq query result against textbox.value

I have the following linq query:

var solicitudes = from s in dbContext.Menores
select s.Solicitud.fiExpEmpleado;

The query results are 41 employees' Ids. My question is, how can I compare these 41 elements against the value of a textbox so that i can restrict user registration (if the ID of the textbox matches with one of the query result the registration is denied)?

Hope your help.

You can write a query that checks whether the value exists:

if (dbContext.Menores.Any(s => s.Solicitud.fiExpEmpleado == someValue))
string text = textbox.Text.Trim();
var solicitudes = (from s in dbContext.Menores
                  where s.FieldToCheck == text
                  select s.Solicitud.fiExpEmpleado).FirstOrDefault();
if (solicitudes != null)
{
    //Deny
}

If solicitudes is being returned as a list of int s you could just to:

int employeeId = Convert.ToInt32(txtMyTextBox.Text);
bool isValidEmployeeId = solicitudes.Any(employeeId);

You don't have to compare all values, just create linq query which query for the value of the textbox, and then count use count method,if count is greater then zero, it means it exits,and you can then deny the user.

Here you go.

   if (dbContext.Menores.Exists(x => x.FieldToCheck == text))
   {
            //deny
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM