How to add day to date in Linq to SQL

前端 未结 10 1094
栀梦
栀梦 2020-12-16 13:53

I am writing this code. Here dt is input into the function, as well as someint. The column Exp is a T-SQL date column, which comes as a DateTime through Linq.



        
相关标签:
10条回答
  • 2020-12-16 14:19

    If you are using the Entity Framework, use the System.Data.Objects.EntityFunctions as below:

    c.CMT_TS > System.Data.Objects.EntityFunctions.AddDays(e.Call.CALL_TS, 1)
    
    0 讨论(0)
  • 2020-12-16 14:24

    May be good idea if you would first change your Sql query to Linq and process linq query instead.

    Just add ToList() or limit record

    return (from a in dataContext.TableOfA.ToList()
       where a.name == "Test" &&
       a.Exp.Value.AddDays(Convert.ToDouble(Someint)) >= new DateTimeOffset(dt)
       select a).First();
    

    So once you have all your stuff in Linq then you can process more effectivly I think.

    Cheers

    0 讨论(0)
  • 2020-12-16 14:29

    You can also use SqlMethods.DateDiffDay method, if you're trying to compare two dates

    0 讨论(0)
  • 2020-12-16 14:30

    I just changed the column back to a DateTime.

    0 讨论(0)
  • 2020-12-16 14:32

    You can use: System.Data.Entity.DbFunctions.AddDays(YourDateTime, NumberOfDays) This function will be translated to valid SQL Query. Class System.Data.Entity.Core.Objects.EntityFunctions which also contains AddDays function is obsolete.

    0 讨论(0)
  • 2020-12-16 14:33

    You can move the call to AddDays from the SQL part to the .NET part:

    a.Exp.Value >= new DateTimeOffset(dt).AddDays(-Convert.ToDouble(Someint))
    
    0 讨论(0)
提交回复
热议问题