简体   繁体   English

如何将 linq 查询结果与 textbox.value 进行比较

[英]How to compare linq query result against textbox.value

I have the following linq query:我有以下 linq 查询:

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

The query results are 41 employees' Ids.查询结果为 41 个员工的 ID。 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)?我的问题是,如何将这 41 个元素与文本框的值进行比较,以便我可以限制用户注册(如果文本框的 ID 与查询结果之一匹配,则注册被拒绝)?

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列表的形式返回solicitudes ,您可以:

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.您不必比较所有值,只需创建 linq 查询来查询文本框的值,然后使用 count 方法进行计数,如果计数大于零,则表示退出,然后您可以拒绝用户。

Here you go.这里是 go。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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