英文:
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<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;
}
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 'DateTime?' does not contain a definition for 'Date' and no accessible extension method 'Date' accepting a first argument of type 'DateTime?' could be found (are you missing a using directive or an assembly reference?)
Then Date is highlighted in
.Where(x => x.CreatedDate.Date >= filter.StartDate && x.CreatedDate.Date <= filter.EndDate)
How do I resolve this?
答案1
得分: 0
DateTime?
相当于 Nullable<DateTime>
。
Nullable<T>
没有 .Date
属性。
你应该写:
x.CreatedDate.Value.Date
但是你应该记住,x.CreatedDate
在运行时可能为 null
。在这种情况下,你将会得到一个 NullReferenceExpection
。
所以,你的逻辑应该支持这种情况,并应包含额外的检查,以确保在 x.CreatedDate
为 null
时不调用 x.CreatedDate.Value.Date
。
有用的链接:
英文:
DateTime?
is an equivalent of Nullable<DateTime>
.
Nullable<T>
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论