我正在尝试从数据表中选择一行,该行具有特定帐号且最近日期早于或等于设置的日期时间。我想要下面但只有最新的条目所以我似乎想在 currentDateTime 之后使用 FirstOrDefault() 或 SingleOrDefault() (即...... <= currentDateTime.FirstOrDefault())
DateTime currentDateTime = firstDate;
while (firstDate <= lastDate)
{
foreach (AccountCategory a in db.AccountCategories)
{
var result = Settings.dtCurrent.AsEnumerable()
.Where(b => b.Field<string>("Account") == a.Account
&& (b.Field<DateTime>("Date") <= currentDateTime);
}
}
上面没有给我最新的条目。
最佳答案
这样试试
DateTime currentDateTime = firstDate;
while (firstDate <= lastDate)
{
foreach (AccountCategory a in db.AccountCategories)
{
var result = Settings.dtCurrent.AsEnumerable()
.Where(b => b.Field<string>("Account") == a.Account
&& (b.Field<DateTime>("Date") <= currentDateTime).OrderByDescending(b=> b.Field<DateTime>("Date")).FirstOrDefault();
}
}
关于c# - 从具有多个条件的数据表中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21154115/