如何在WHERE子句(LINQ)中使用DateTime?

Wah*_*eed 2 c# linq linq-to-sql

如何添加日期是where子句.我正在使用以下代码.

    var opportunites = from opp in this.DataContext.Opportunities
                            join org in this.DataContext.Organizations on opp.OrganizationID
                            equals org.OrgnizationID
                            select new
                            {
                                org.CreatedDate,
                                org.OrganizationName
                            };
     if (createdDate != string.Empty)
         opportunites = opportunites.Where(opp => 
                            opp.CreatedDate == Convert.ToDateTime(createdDate));
Run Code Online (Sandbox Code Playgroud)

但是使用这段代码我没有返回任何结果.我认为错误是使用==运算符.

我想我应该有这样的代码

"where createdDate between" + createdDate + "00:00:00" + and createdDate + "23:59:59"
Run Code Online (Sandbox Code Playgroud)

如何在LINQ中归档这个东西.

有什么建议??

Bru*_*uce 7

您可以按如下方式仅提取DateTime的"日期"部分:

opp.CreatedDate.Date == Convert.ToDateTime(createdDate).Date
Run Code Online (Sandbox Code Playgroud)