i'm a junior developer and trying to convert the following linq statement to T-Sql:
var results = context
.Employees
.Where(item => (dateFilter.ToUpper() == "P") ? item.PublishedDate >= From : item.CapturedDate >= dateFromDT)
.Where(item => (dateFilter.ToUpper() == "P") ? item.PublishedDate <= dateTo : item.CapturedDate <= dateTo)
.FirstOrDefault()
Can one please help me?
Select * from Employees where PublishedDate = From and .......
I think it should be like this.
Select Top 1 * from Employees
where
(dateFilter = "P" COLLATE Latin1_General_CS_AS AND PublishedDate >= From AND PublishedDate >=dateTo)
OR
(dateFilter != "P" COLLATE Latin1_General_CS_AS AND CapturedDate >= dateFromDT AND CapturedDate >= dateTo)
You could use something similar to this:
select top 1 *
from employees
where (upper(DateFilter) = 'P' and PublishedDate >= From and PublishedDate <= dateTo)
or (upper(DateFilter) <> 'P' and CapturedDate >= dateFromDT and CapturedDate <= dateTo)
if DateFilter is a column in the table then it is better to skip the upper as it can degrade performance (given that you have a index on that column).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.