linq to sql select as命令

时间:2013-05-22 11:53:00

标签: c# sql database linq

我有类似这样的SQL查询:

SELECT 
((dbo.ActiveWO.StartTime*60*60*24) - dbo.ActiveWOUpdatedTimes.ElapsedTime) as TimeLeft

如何将其插入到linq to sql查询中?

基本上不确定AS的语法是什么以及如何在c#查询中实际编写上面的计算然后我需要通过TimeLeft命令。

2 个答案:

答案 0 :(得分:5)

从您的previous question,我猜你有一个类似下面的查询。您可以使用select new创建anonymous type object,然后使用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); 

答案 1 :(得分:1)

使用anonymous types

var results = ... //your query
              .Select(a => new 
               {
                  TimeLeft = a.StartTime * 60 * 60 * 24 - a.ElapsedTime
               });