表达式不能包含查询表达式 [英] expression cannot contain query expressions

查看:159
本文介绍了表达式不能包含查询表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中有以下代码。其中我只想要单个员工数据

i have following code in C#.where i want only single employee data

Employee ObjEmployee = new Employee();
ObjEmployee.GetAllEmployee().Where(emp => emp.EmployeeCode == txtempcode.Text);



但它会抛出以下错误

表达式不能包含查询表达式


but it throws following error
expression cannot contain query expressions

推荐答案

我们无法分辨:你需要开始查看GetAllEmployeee做了什么,以及它返回什么。

使用调试器进入你的代码,或者查看异常发生的时间以及它正在抱怨的特定行。



我们不能:我们无法访问您的屏幕,硬盘或数据 - 更不用说您的代码了!
We can't tell from that: you need to start looking at what GetAllEmployeee does, and what it returns.
Use the debugger to step into your code, or to look at the exception when it occurs and at teh specific line it is complaining about.

We can't: we don;t have access to your screen, HDD, or data - much less your code!


嗨Kladane,



您只需要指定查询的返回类型。

这是一个例子。



Hi Kladane,

Your Only have to specify the return type of the query.
Here is an example.

Employee ObjEmployee = new Employee();
ObjEmployee = ObjEmployee.GetAllEmployee().Where(emp => emp.EmployeeCode == txtempcode.Text).FirstOrDefault(); //allows you to get the first user that matches your condition. 

.SingleOrDefault() //allows you to get the UNIQUE user that matches your condition. if your there is more than one that matches your condition, an exception will be thrown.

.ToList() // you are selecting a list of employee. in this case, you have to declare a list of //employee instead of a single instance.
List<employee> ObjEmployeeList = new List<employee>();
ObjEmployeeList = ObjEmployeeList.GetAllEmployee().Where(emp => emp.EmployeeCode == txtempcode.Text).ToList(); 

</employee></employee>





一个细节想法,根据你的方法(GetAllEmployee)代码,语法可能不正确。

我通常将GetAll方法写为静态,因此它与实例无关。

在这种情况下。代码将是这样的。





One Detail thought, the syntax may be incorrect based on your method(GetAllEmployee) code.
I generaly write GetAll method as static so it is independent form the instance.
In that case. the code will be like this.

List<employee> ObjEmployeeList = new List<employee>();
ObjEmployeeList = Employee.GetAllEmployee().Where(emp => emp.EmployeeCode == txtempcode.Text).ToList(); 
</employee></employee>







希望有所帮助




Hope it helps


这篇关于表达式不能包含查询表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆