Linq之间的约会

时间:2017-05-31 18:07:50

标签: c# entity-framework linq linq-to-sql

所以我有一个正确获取数据的查询但是当我尝试过滤掉日期之间的linq语句时,我一直得到零结果。

        var query= Enumerable.Empty<CustomClass>().AsQueryable();

        query= (from auto in db.AutoInvs
            join deal in db.Deals on new { inv = auto.INVUID, client = auto.CLIENTID, acct = auto.ACCOUNT} equals new {inv = deal.INVUID,client = deal.CLIENTID, acct = (int?) deal.ACCOUNT}
            join dmCust in db.DMCusts on new {inv = auto.INVUID, client = auto.CLIENTID, acct = auto.ACCOUNT.ToString()} equals  new {inv = dmCust.INVUID, client = dmCust.CLIENTID, acct = dmCust.ACCOUNT} 
            join act in db.Acts on new { inv = auto.INVUID, client = auto.CLIENTID, acct = auto.ACCOUNT.ToString()} equals new { inv = act.INVUID, client = act.CLIENTID, acct = act.Key }
            where auto.DATAPROCESSEDDATE == null && auto.INVUID != "" 
            select new CustomClass()
            {
                AutoInv = auto,
                Deal = deal,
                DmCust = dmCust,
                Act = act
            });
    var filteredData = query.Where(c => c.AutoInv.DATESOLD >= dateFrom.Value && c.AutoInv.DATESOLD <= dateTo.Value).AsQueryable();
    Console.WriteLine(filteredData.ToList().Count);

1 个答案:

答案 0 :(得分:2)

使用这个精简版(和我自己的数据),它对我有用:

var query = Enumerable.Empty<Tbl1>().AsQueryable();

DateTime? dateFrom = new DateTime(2017, 5, 10);
DateTime? dateTo = new DateTime(2017, 5, 20);

query = (from auto in db.Tbl1s
         select auto);

var filteredData = query.Where(c => c.StartDate >= dateFrom.Value 
                                 && c.StartDate <= dateTo.Value).AsQueryable();
filteredData.Dump();

您可能遇到的一个问题是dateTo将是独占的终点---除非DATESOLD正好是午夜,否则您将无法获得任何结果在结束日期。