【问题标题】:How to convert temp table with case statement SQL to Linq?如何将带有case语句SQL的临时表转换为Linq?
【发布时间】:2013-06-04 08:35:36
【问题描述】:

我是Linq的新手,不知道如何解决MS SQL查询中的一些问题。

Select e.EmployeeID
, e.EmployeeName
, e.Division
, e.DepartmentCode
, e.DesignationGrpCode
Into #temp
from chr.dbo.EMPLOYEE e
Where e.DepartmentCode in ( 
                        Select DepartmentCode 
                        from chr.dbo.EMPLOYEE 
                        where EmployeeID = 'S-1287'
                        )

Select * from #temp t
where t.EmployeeID in (
                        Case When t.Division <> 'CHR' Then (Select EmployeeID from #temp where DesignationGrpCode = 'CorpDMGR')
                        Else (Select EmployeeID from #temp where DesignationGrpCode = 'MGT') End)
Drop table #temp

如何将此查询转换为 Linq?请

这是我的 linq 语句

var employee = new Common().Employee();

        var jobdesc = new Common().JobHistory();

        List<EMPLOYEE> dCode = (from e in employee
                              where e.EmployeeID == "S-1204"
                              select new EMPLOYEE
                              {
                                  DepartmentCode = e.DepartmentCode
                              }).ToList();

        List<EMPLOYEE> emp = (from e in employee
           join d in dCode on e.DepartmentCode equals d.DepartmentCode into temp
           from t in temp.DefaultIfEmpty()


           orderby e.EmployeeName
           select new EMPLOYEE
           {
               EmployeeID = e.EmployeeID,
               EmployeeName = e.EmployeeName
           }).ToList();

但不知道如何检查 CASE ELSE 语句

【问题讨论】:

  • 请添加您创建的 LINQ,但没有按照您的要求进行操作。
  • @Bazzz 您好,请检查,已更新!

标签: c# sql-server asp.net-mvc linq linq-to-entities


【解决方案1】:

试试这个。它在ideone 中编译和运行,但未经测试,祝你好运。

var temp = from e in context.EMPLOYEE
           where (from f in context.EMPLOYEE
                  where f.EmployeeID == "S-1287"
                  && e.DepartmentCode == f.DepartmentCode
                  select f).Any()
           select e;

var result = from t in temp
             where (from u in temp
                    where u.DesignationGrpCode
                          == (t.Division == "CHR" ? "CorpDMGR" : "MGT")
                    && t.EmployeeID == u.EmployeeID
                    select u).Any()
             select t;

假设您指向 SQL Server 2005 或更高版本,我希望 LINQ 足够聪明,可以将临时表转换为 WITH 子句。

更新:由于temp 的子查询几乎可以肯定返回单个值,因此您应该(假设 EmployeeID 是主键)将其分开:

string deptForS1287 = (from e in context.EMPLOYEE
                       where e.EmployeeID == "S-1287"
                       select DepartmentCode).Single();

var temp = from e in context.EMPLOYEE
           where e.DepartmentCode == deptForS1287
           select e;

⋮

如果 EMPLOYEE 表未提及员工 S-1287,这具有引发异常的附带好处。

【讨论】:

  • 你好兄弟,你的代码看起来很完美,我用了这个,没有错误但没有数据出来。真是一头雾水!兄弟你有什么想法吗?
  • @CMMaung:把它分解成更小的部分。首先检查temp 中是否有任何内容。如果有,检查result的子查询是否有数据,以此类推。如果没有,则检查temp 的子查询是否返回任何内容。
  • @CMMaung:我刚刚意识到原始查询中有一个错字。 temp 子查询在 EmployeeID 上匹配,它应该在 DepartmentCode 上匹配。我修复了它并添加了可能的改进。
  • 感谢您的热心帮助。现在我知道为什么前一个没有返回记录了。因为它只返回你提到的单个值。现在明白了!再次感谢。你让我开心:)
【解决方案2】:

在极端情况下,你可以做到:

using (var context = new ConnectDb())    
    {
         context.ExecuteStoreCommand("your query");
         // for exaple: context.ExecuteStoreCommand("TRUNCATE TABLE Peoples"); 
         context.SaveChanges();
    }

【讨论】:

  • 您的答案中没有 LINQ。这不是 OP 想要的。
  • @Rim4irok 感谢您的信息。似乎是解决方案之一:D
猜你喜欢
  • 2021-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-24
  • 2021-12-17
  • 2020-01-24
  • 1970-01-01
  • 2013-02-15
相关资源
最近更新 更多