如何在Linq C#中编写当前的SQL语句 [英] How do I write the current SQL statement in Linq C#

查看:126
本文介绍了如何在Linq C#中编写当前的SQL语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我刚刚开始使用Lightswitch和Linq,并且很难编写以下简单的SQL语句作为Linq查询:

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:

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



有2个表:部门和员工.每个员工的确有薪水,我想对列表中每个部门的总薪水费用求和.

我读到我不能在Lightswitch的Linq查询中使用联接.我的问题是如何在Linq中实现这一目标?

提前谢谢!

Dave



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

推荐答案


您可以使用以下查询.由于有2个表,部门和员工

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

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.


这篇关于如何在Linq C#中编写当前的SQL语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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