【问题标题】:how to write the following sql query in linq如何在linq中编写以下sql查询
【发布时间】:2017-11-14 13:25:03
【问题描述】:
select 
    coalesce(x.value,a.default_value) as value 
from table1 a 
left outer join 
    (
       select 
          b.configid,
          b.value 
       from table2 b 
       join table3 c on b.dataid=c.id 
       where c.id=0
    )x on a.id=x.configid 
where a.key='abc'

【问题讨论】:

标签: sql linq


【解决方案1】:

用于将 SQL 转换为 LINQ,

  1. 将子选择转换为单独的变量
  2. 按 LINQ 子句顺序翻译每个子句,将一元运算符(DISTINCT、TOP 等)作为应用于整个 LINQ 查询的函数。
  3. 使用表别名作为范围变量。使用列别名作为匿名类型字段名称。
  4. 对多列使用匿名类型(新 { })
  5. 左连接是通过使用一个连接变量来模拟的,然后从连接变量执行另一个操作,然后使用 .DefaultIfEmpty()。
  6. coalesce 替换为条件运算符和空测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    相关资源
    最近更新 更多