Click here to Skip to main content
16,020,459 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have following code in C#.where i want only single employee data
C#
Employee ObjEmployee = new Employee();
ObjEmployee.GetAllEmployee().Where(emp => emp.EmployeeCode == txtempcode.Text);

but it throws following error
expression cannot contain query expressions
Posted
Updated 21-May-14 22:18pm
v2
Comments
DamithSL 22-May-14 4:25am    
is this VS 2010? are you trying to evaluate expression while debugging and get this exception?
Maciej Los 22-May-14 5:10am    
Do not repost!
http://www.codeproject.com/Questions/776524/expression-cannot-contain-lambda-expressions

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!
 
Share this answer
 
Hi Kladane,

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

C#
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>


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.

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



Hope it helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900