Difference Of Dates in Linq To Entities

Posted by Sharpcnet under LINQ on 12/5/2013 | Points: 10 | Views : 8688 | Status : [Member] | Replies : 7
I am trying to get the no. of days by subtracting 2 dates, but both these trials say - 'DataSet does not support system.Nullable<> - Using C#, Linq To Entities

public DataTable GetData()
{
var qry = from a in MyDB.tblBranch.Where(a=>a.branchid==1)
join e in MyDB.tblEmp on a.Eid equals e.Eid
select new
{
days = System.Data.Objects
.EntityFunctions.DiffDays(a.ExitDate,a.EnterDate)
//days = System.Data.Objects.SqlClient.SqlFunctions.
//DateDiff("dd",a.ExitDate,a.EnterDate)
};
DataTable dt = qry.ToDataTable();
return dt;
}





Responses

Posted by: Bandi on: 12/5/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
http://stackoverflow.com/questions/570858/linq-to-entities-for-subtracting-2-dates

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sharpcnet on: 12/5/2013 [Member] Starter | Points: 25

Up
0
Down
Thank you but none of them helped. Could you suggest the code that could go with my case. I am unable to figure it out in anyways


Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/5/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer:-

http://stackoverflow.com/questions/570858/linq-to-entities-for-subtracting-2-dates
http://stackoverflow.com/questions/12247727/subtract-two-dates-in-linq-to-entities-which-returns-a-timespan
http://itsmebhavin.wordpress.com/2011/03/03/linq-to-entities-for-subtracting-2-dates/

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/5/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
http://www.questionhub.com/StackOverflow/570858
http://www.pressinganswer.com/1090726/linq-to-entities-for-subtracting-2-dates
http://www.plaintxt.org/linq_to_entities_for_subtracting_dates_21377296.html

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 12/5/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Sample code to get number of days between two dates and checking for NULL values
int totalDays = 
(from d in db.RentalDvds.Where(d=>d.ReturnDate.HasValue && d.RentedDate.HasValue)
select (d.ReturnedDate - d.RentedDate).Days).FirstOrDefault();


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/6/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
DateTime oldestDate = DateTime.Now.Subtract(new TimeSpan(maxAgeInDays, 0, 0, 0, 0));

Or
where (vid.CreatedDate >= EntityFunctions.AddDays(DateTime.Now, -maxAgeInDay))

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sharpcnet on: 12/6/2013 [Member] Starter | Points: 25

Up
0
Down
@Bandi.. Hi. It might work if it was the only column, but I have multiple columns to retrieve in the datatable and the days would be one of them. What should I write in that case:
select new
{
days = System.Data.Objects.EntityFunctions.DiffDays(a.ExitDate,a.EnterDate)
branchid = a.branchid,
entrydate = a.entrydate ?? datetime.minvalue,
exitdate = a.exitdate ?? datetime.minvalue,
};


Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response