如何在实体框架中实现选择案例?
我在 SQL 中使用 CASE 进行查询,如何使用实体框架进行查询?
DECLARE @SystemTypeId int
SELECT @SystemTypeId = [SystemTypeId] FROM [Properties] WHERE [Id] = @PropertyId
SET @RETURN_VAL =
CASE @SystemTypeId
WHEN 2 THEN (SELECT [Created] FROM [Assets] WHERE [Id] = @AssetId)
WHEN 3 THEN (SELECT dbo.GetAssetValueById([CreatedBy])
FROM [Assets]
WHERE [Id] = @AssetId)
WHEN 9 THEN (SELECT [LastModified]
FROM [Assets]
WHERE [Id] = @AssetId)
ELSE NULL
END
I have a query with CASE in SQL, how can I do it with Entity Framework?
DECLARE @SystemTypeId int
SELECT @SystemTypeId = [SystemTypeId] FROM [Properties] WHERE [Id] = @PropertyId
SET @RETURN_VAL =
CASE @SystemTypeId
WHEN 2 THEN (SELECT [Created] FROM [Assets] WHERE [Id] = @AssetId)
WHEN 3 THEN (SELECT dbo.GetAssetValueById([CreatedBy])
FROM [Assets]
WHERE [Id] = @AssetId)
WHEN 9 THEN (SELECT [LastModified]
FROM [Assets]
WHERE [Id] = @AssetId)
ELSE NULL
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用
? 时,Linq-to-Entities 不会为您生成 CASE 语句,除非是二进制 CASE 语句。 :
运算符。您可以在两个查询中运行它:首先获取 SystemTypeId,然后运行适当的查询。
或者,您可以绕过 Linq 并运行原始 SQL,如果您有复杂的查询,这通常是最好的方法。
Linq-to-Entities won't generate a CASE statement for you, other than a binary CASE statement when you use the
? :
operator.You can either run it in two queries: get the SystemTypeId first, then run the appropriate query.
Or you can bypass Linq and run the raw SQL, which is often the best approach if you have a complex query.