Error CS1061 'DateTime?' does not contain a definition for 'Date' and no accessible extension method 'Date'

huangapple go评论68阅读模式
英文:

Error CS1061 'DateTime?' does not contain a definition for 'Date' and no accessible extension method 'Date'

问题

在我的ASP.NET Core Web API中,我有以下的代码。

public class PagingDto
{
    public int PageSize { get; set; }
    public int PageNumber { get; set; }
}

public class TransactionFilter : PagingDto
{
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public string SearchQuery { get; set; }
}

public IQueryable<Transaction> GetAllTransactions(TransactionFilter filter)
{
    var transactions = _context.Transactions
        .Where(x => x.CreatedDate.Date >= filter.StartDate && x.CreatedDate.Date <= filter.EndDate)
        .Where(x => string.IsNullOrEmpty(filter.SearchQuery) || x.Customer.Name.ToLower().Contains(filter.SearchQuery.ToLower())
        || x.Payment.Status.ToLower().Contains(filter.SearchQuery.ToLower())
        || x.Payment.TransactionReference.ToLower().Contains(filter.SearchQuery.ToLower())
        || x.Payment.MethodOfPayment.ToLower().Contains(filter.SearchQuery.ToLower())
        || x.Payment.Amount.ToString().Contains(filter.SearchQuery))
        .Include(b => b.Payment)
        .Include(b => b.Customer)
        .OrderByDescending(transaction => transaction.CreatedDate);
    return transactions;
}

我想查询所有在StartDate和EndDate之间使用CreatedDate的交易。

但是我得到了这个错误:

错误 CS1061:'DateTime?'不包含名称为'Date'的定义,也没有找到可接受第一个参数类型为'DateTime?'的扩展方法'Date'(您是否缺少using指令或程序集引用?)

然后在这行中,Date 被突出显示:

.Where(x => x.CreatedDate.Date >= filter.StartDate && x.CreatedDate.Date <= filter.EndDate)

如何解决这个问题?

英文:

In my ASP.NET Core Web API, I have this code.

public class PagingDto
{
    public int PageSize { get; set; }
    public int PageNumber { get; set; }
}

public class TransactionFilter : PagingDto
{
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public string SearchQuery { get; set; }
}

GetAllTransactions

public IQueryable&lt;Transaction&gt; GetAllTransactions(TransactionFilter filter)
{
    var transactions = _context.Transactions
        .Where(x =&gt; x.CreatedDate.Date &gt;= filter.StartDate &amp;&amp; x.CreatedDate.Date &lt;= filter.EndDate)
        .Where(x =&gt; string.IsNullOrEmpty(filter.SearchQuery) || x.Customer.Name.ToLower().Contains(filter.SearchQuery.ToLower())
    || x.Payment.Status.ToLower().Contains(filter.SearchQuery.ToLower())
    || x.Payment.TransactionReference.ToLower().Contains(filter.SearchQuery.ToLower())
    || x.Payment.MethodOfPayment.ToLower().Contains(filter.SearchQuery.ToLower())
    || x.Payment.Amount.ToString().Contains(filter.SearchQuery))
        .Include(b =&gt; b.Payment)
        .Include(b =&gt; b.Customer)
        .OrderByDescending(transaction =&gt; transaction.CreatedDate);
    return transactions;
}

I want to Query all transactions that falls between StartDate and EndDate using CreatedDate.

But I got thus error:

Severity	Code	Description	Project	File	Line	Suppression State
Error	CS1061	&#39;DateTime?&#39; does not contain a definition for &#39;Date&#39; and no accessible extension method &#39;Date&#39; accepting a first argument of type &#39;DateTime?&#39; could be found (are you missing a using directive or an assembly reference?)

Then Date is highlighted in

.Where(x =&gt; x.CreatedDate.Date &gt;= filter.StartDate &amp;&amp; x.CreatedDate.Date &lt;= filter.EndDate)

How do I resolve this?

答案1

得分: 0

DateTime? 相当于 Nullable<DateTime>

Nullable<T> 没有 .Date 属性。

你应该写:

x.CreatedDate.Value.Date

但是你应该记住,x.CreatedDate 在运行时可能为 null。在这种情况下,你将会得到一个 NullReferenceExpection

所以,你的逻辑应该支持这种情况,并应包含额外的检查,以确保在 x.CreatedDatenull 时不调用 x.CreatedDate.Value.Date

有用的链接:

英文:

DateTime? is an equivalent of Nullable&lt;DateTime&gt;.

Nullable&lt;T&gt; doesn't have a property .Date.

You should write:

x.CreatedDate.Value.Date

But you should keep in mind that x.CreatedDate can be null in the runtime. In that case, youll get a NullReferenceExpection`.

So, your logic should support such a case and should contain additional checks not to call x.CreatedDate.Value.Date when x.CreatedDate is null.

> Useful links:
> - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/nullable-value-types

huangapple
  • 本文由 发表于 2023年2月27日 02:22:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定