gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:14:03 24 4
gpt4 key购买 nike

如何添加日期是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));

但是使用这段代码我不会返回任何结果。我认为错误在于 == 运算符。

我想我应该有这样的代码

"where createdDate between" + createdDate + "00:00:00" + and createdDate + "23:59:59"

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

有什么建议吗?

最佳答案

您可以只提取 DateTime 的“日期”部分,如下所示:

opp.CreatedDate.Date == Convert.ToDateTime(createdDate).Date

关于c# - 如何在 WHERE 子句 (LINQ) 中使用 DateTime?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1005154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com