简体   繁体   中英

Difference Of Dates in Linq To Entities

I am trying to get the no. of days by subtracting 2 dates, but both these trials say - 'DataSet does not support system.Nullable<>

public DataTable GetData()
{
    var qry = from a in MyDB.tblBranch.Where(a.EntryDate.HasValue && 
              a.exitdate.hasvalue)
              join e in MyDB.tblEmp on a.Eid equals e.Eid
              select new
              {
                 id = a.branchid,
                 name = e.empname,
                 days = System.Data.Objects
                        .EntityFunctions.DiffDays(a.ExitDate,a.EnterDate)

              };
     DataTable dt = qry.ToDataTable();
     return dt;
}

EntityFunctions.DiffDays() returns Nullable<int> , so you have to deal with null results

for example

qry
    .Where(d => d.days.HasValue)
    .Select(d => d.Value)

edit

try smth like that

var qry = MyDB.tblBranch
    .Where(a=>a.branchid==1)
    .Select(a => new
    {
        days = System.Data.Objects
            .EntityFunctions.DiffDays(a.ExitDate,a.EnterDate)
    })
    .Where(d => d.days.HasValue)
    .Select(d => d.Value);

DataTable dt = qry.ToDataTable();

if you want to keep null values then you can do smth like this

var qry = MyDB.tblBranch
    .Where(a=>a.branchid==1)
    .Select(a => new
    {
        days = System.Data.Objects
            .EntityFunctions
            .DiffDays(a.ExitDate,a.EnterDate)
    })
    .Select(b => b.HasValue ? (Object) b.Value :  DBNull.Value);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM