我有一个类似这样的 SQL 查询:
SELECT
((dbo.ActiveWO.StartTime*60*60*24) - dbo.ActiveWOUpdatedTimes.ElapsedTime) as TimeLeft
如何将其插入到 linq to sql 查询中?
基本上不知道 AS 的语法是什么以及如何在 c# 查询中实际编写上面的计算,然后我需要按 TimeLeft 排序。
从您之前的问题中,我猜您有如下查询。您可以使用select new
创建一个匿名类型对象,然后使用OrderBy
如下:
var query = (from activeWO in context.ActiveWOs
join activeWOUpdated in context.ActiveWOUpdatedTimes on activeWO.PW_ID equals activeWOUpdated.PW_ID into dj
from activeWOUpdated in dj.DefaultIfEmpty()
where activeWO.WODC.Contains("IDC")
select new
{
TimeLeft = activeWO.StartTime * 60 * 60 * 24 - dj.ElapsedTime
}).Orderby(r=> r.TimeLeft);
使用匿名类型:
var results = ... //your query
.Select(a => new
{
TimeLeft = a.StartTime * 60 * 60 * 24 - a.ElapsedTime
});