LinqToSql中的子查询

时间:2017-11-24 20:08:04

标签: c# sql sql-server entity-framework

我有几张桌子(浓缩为:)

Bookings
+--------+------+------------+
| BKName | BkId | BkActivity |
+--------+------+------------+
Functions
+-------+--------+------+-----+-------+---------+---------+
| FName | FStart | FEnd | FID | FBkId | FRmCode | FEvents |
+-------+--------+------+-----+-------+---------+---------+
Rooms
+--------+--------+
| RmName | RmCode |
+--------+--------+
F2
+----------+------+
| UniqueId | Item |
+----------+------+
B2
+----------+------+
| UniqueId | Item |
+----------+------+

在SQL中,我的查询是

Select b.BkName
       r.RmName
       f.FName
       f.FStart
       f.FEnd
       f.FID
From Bookings as b
    Inner Join Functions as f
        on b.BkId = f.BkId
    Left Outer Join Rooms as r
        on f.FRmCode = r.RmCode
Where (b.BkActivity <> 'C') and (f.FEvents = 1)
and isnull((Select Item
            from F2
            where UniqueId = F.FID),0)=0
and isnull((Select Item
            from B2
            where UniqueId = b.BkId),0)=0

我正在尝试使用Enity Framework和扩展LINQtoSQL,但我有一段时间获得相同的信息。我相信我的问题是两个子查询弄乱了东西和左外连接。我试过的所有变化最终都没有返回。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码:

where

了解列的类型会很有帮助。我还尝试优化extension String { var htmlDecoded: String { let decoded = try? NSAttributedString(data: Data(utf8), options: [ .documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue ], documentAttributes: nil).string return decoded ?? self } } 子句中的查询,但这需要更多的数据库知识。请记住,此代码可能不如SQL存储过程高。如果性能对您很重要,我建议您记录SQL输出并测量执行选择所需的时间。