gpt4 book ai didi

c# - 如何从 LINQ 实现 SQL CASE 语句

转载 作者:行者123 更新时间:2023-11-30 23:08:24 26 4
gpt4 key购买 nike

我是 LINQ 的新手,我想知道我是否可以从 LINQ 实现以下 SQL 查询?

我正在使用 Entity Framework Core。

SELECT 0 [All], [Range] =
CASE
WHEN Value BETWEEN 0 AND 25 THEN 'Low'
WHEN Value BETWEEN 25 AND 75 THEN 'Medium'
WHEN Value BETWEEN 75 AND 90 THEN 'High'
WHEN Value BETWEEN 90 AND 100 THEN 'Very High'
END
FROM Result.Calculation C
INNER JOIN Data.SampleSet S ON C.SampleSetID = S.ID
WHERE S.SampleDrawn >= DATEADD(MONTH,-3,GETDATE()) AND S.Department = 'LOCATION A'

目前,我正在使用如下所示的 FromSql 来调用存储过程。我想知道我是否可以在不使用存储过程的情况下做同样的事情?

var result = context.MyData.FromSql("data.GetMyData @pType = {0}, @pLocation = {1}, @pNoOfDays = {2}, @pStartDate = {3}, @pEndDate = {4}", type, location, noOfDays, startDate, endDate).ToList();

谢谢。

最佳答案

你可以使用它。

from C in Calculations
join S in SampleSets on C.SampleSetID equals S.ID
where S.SampleDrawn >= DateTime.Now.AddMonths(-3)
&& S.Department == "LOCATION A"
select new {
All = 1
, Range =
(C.Value >= 0 && C.Value < 25) ? "Low" :
(C.Value >= 25 && C.Value < 75) ? "Medium" :
(C.Value >= 75 && C.Value < 90) ? "High" :
(C.Value >= 90 && C.Value <= 100) ? "Very High" : null
}

关于c# - 如何从 LINQ 实现 SQL CASE 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46560686/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com