Linq to sql c#Lamba传递日期

时间:2017-02-15 17:28:01

标签: sql linq datetime linq-to-sql linq-to-entities

我希望能够传递mm-dd-yyyy(2-17-2017)的日期,因为sql server datetime适用于此。

所以我正在为sql查询编写lambda linq,但我一直在努力学习语法

var query = QALists
    //.Where (z => z.Disposition == "Verified")
    //.Where (z => z.CallDate == DateTime.ParseExact("20170215","MMddyyyy",System.Globalization.CultureInfo.InvariantCulture))
    //.Where(z => Convert.ToDateTime(20170215, System.Globalization.CultureInfo.InvariantCulture))
    .Where (z => DateTime.Parse(string.Format("{0}", z.Calldate.ToString() == "2-17-2017")))
    .AsEnumerable()
    .Select(z => new QAList()
                {
                    Agent = z.Agent,
                    ClientName = z.ClientName,
                    Disposition = z.Disposition,
                    CallDate = z.Calldate,
                    Comment = z.Comment,
                    CreatedDateTime = z.CreatedDateTime,
                    CallLength = z.CallLength,
                    IdentityColumnId = z.IdentityColumnId,
                    Number = z.Number,
                    InboundCall = z.InboundCall,
                    OutboundCall = z.OutboundCall,
                    Status = z.Status,
                    QAListId = z.QAListId     
                }).ToList();

我在sql服务器的c#中注释了我的各种测试CallDate看起来像2017-02-09T07:51:00.76

如何使用.Where lambda简单地解​​析传入的日期?

CallDate在poco中看起来像这个属性

   public DateTime CallDate { get; set; }

1 个答案:

答案 0 :(得分:0)

只需使用构造函数来生成日期并使用Date属性DateTime

.Where (z => z.Calldate.Date == new DateTime(2017,2,17).Date)

https://msdn.microsoft.com/en-us/library/system.datetime.date.aspx