Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I've just started working with Lightswitch and Linq and I'm having a hard time to write the following simple SQL statement as a Linq query:

SQL
SELECT Departments.Name, SUM(Salary) AS Total FROM EmployeesSet
JOIN Departments ON EmployeesSet.Department_Employees = Departments.id
GROUP BY Departments.Name


There are 2 tables: Departments and Employees. Each employee does have a salary and I want to SUM the total salary costs for each department in a list.

I've read that I can't use joins in Linq queries in Lightswitch. My question is how to realise this in Linq?

Thanks in advance!

Dave
Posted
Updated 30-Nov-14 12:21pm
v2
Comments
PIEBALDconsult 30-Nov-14 18:41pm    
If it ain't broke, why break it?
BillWoodruff 30-Nov-14 18:53pm    
Assuming "Departments" consists of "rows" each of which contain a set of references to an "EmployeeID:" ... in other words you can only access the employee's salary by doing a look-up from the data in Departments ... would an example showing you how to do this in Linq using two DataTables ... independent of LightSwitch ... be of any use ?

1 solution

Hi
You can use the following query. As there are 2 tables Departments and Employees

C#
using(var context =new dbContext(Your contextName))
{
 var result = from dept in context.tblDepartments 
              group dept by dept.V_DEP_Name into dept1
              join emp in context.tblemployees on dept1.FirstOrDefault().I_DEPT_ID equals emp.I_DEPT_ID
              select new
             {
                    TotalSalary = dept1.sum(p=>p.salary),
                    DepartName = dept1.FirstOrDefault(p=>p.V_DEP_Name)
             }
}

I have given you the example. Here you can make change in the query as column in tables.
 
Share this answer
 
v2
Comments
Maciej Los 1-Dec-14 2:19am    
Looks perfect! +5!
Next time do not forget to format code block!
Member 11275599 1-Dec-14 11:29am    
Thanks a lot, I will try this out.

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