3

I need to get the number of days between two dates from database using Linq Query, please help me how to do this. thanks

I tried the bellow code but it return me the result nul! please help

 DateTime rightNow = DateTime.Now;

        //int totalDays = (from d in db.vuExpiredProjectsAssigned
        //                 where d.AssignedTo==obj.DepartmentID
        //                 select (d.AssignedDate - d.DueDate).Days).FirstOrDefault();

        //var numberdays = (from pd in db.vuExpiredProjectsAssigned
        //                  where pd.AssignedTo == obj.DepartmentID
        //                  select SqlFunctions.DateDiff("day", pd.AssignedDate, rightNow));

       var result =(from dd in db.vuExpiredProjectsAssigned
                    where dd.AssignedTo==obj.DepartmentID
                    select new
                    {
                        days=SqlFunctions.DateDiff("Day",dd.DueDate,rightNow)
                    });

       ViewBag.ndays = result;
6
  • 3
    var days = (DueDate - AssigneDate).Days; Commented Oct 9, 2016 at 7:27
  • the error is " DbArithmeticExpression arguments must have a numeric common type." Commented Oct 9, 2016 at 7:49
  • 1
    Materialize the query first Commented Oct 9, 2016 at 9:00
  • check my updated code Commented Oct 11, 2016 at 6:23
  • your problem is solved? Commented Oct 11, 2016 at 8:13

3 Answers 3

4

Arithmetic with DateTime is not supported in Entity Framework. You have to use DbFunctions*. So, for the first part of your statement, something like:

var numberdays = ( from p in db.vuExpiredProjectsAssigned
                     where p.AssignedTo == obj.DepartmentID
                     select DbFunctions.DiffDays(p.AssignedDate,p.DueDate));

OR

var numberdays = ( from p in db.vuExpiredProjectsAssigned
                     where p.AssignedTo == obj.DepartmentID
                     select SqlFunctions.DateDiff("day", p.AssignedDate, p.DueDate));

For more reference please check below links.

SqlFunctions Class

DbFunctions Class

Sign up to request clarification or add additional context in comments.

7 Comments

it return an error " No overload for Method 'diffDays' takes 1 arguments " ??
what it retuns? i store the result in ViewBag it print me the query itself
it should return int array. Could you please put the break point and check that?
SELECT DATEDIFF(day, [Extent1].[AssignedDate], [Extent1].[DueDate]) AS [C1] FROM [dbo].[vuExpiredProjectsAssigned] AS [Extent1] WHERE [Extent1].[AssignedTo] = @p__linq__0
could you please convert your result into list
|
0

try with this

DbFunctions.DiffDays(dd.DueDate,rightNow).Value

Update

var rightNow= DateTime.Today.ToShortDateString();

public class democlass
{
 public int count {get; set;}
}

and query like this

var result =(from dd in db.vuExpiredProjectsAssigned
                where dd.AssignedTo==obj.DepartmentID
                select new democlass()
                {
                    count = DbFunctions.DiffDays(dd.DueDate,rightNow).Value
                });

then check your result variable.

9 Comments

var result = (from dd in db.vuExpiredProjectsAssigned where dd.AssignedTo == obj.DepartmentID select DbFunctions.DiffDays(dd.AssignedDate, dd.DueDate).Value); ViewBag.ndays = result;
still the return result is null
days is anonymous field?
i have a date stored in database using short format. ex . 2016-1-1,
i did but still result: null
|
0

As long as you are not using the difference in days in the where clasuse, then it is better to do this calculation in your code instead of adding overhead to SQL, so change your code to:

public class democlass
{
 public DateTime DueDate{get;set;} 
 public int count {get; set;}
}

 var result =((from dd in db.vuExpiredProjectsAssigned
            where dd.AssignedTo==obj.DepartmentID
            select new democlass()
            {
                DueDate= dd.DueDate;
            })).ToList();

result.ForEach(a=> {a.count = a.DueDate.Subtract(DateTime.Now).TotalDays});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.