c# - 从具有多个条件的数据表中选择

标签 c# linq

我正在尝试从数据表中选择一行,该行具有特定帐号且最近日期早于或等于设置的日期时间。我想要下面但只有最新的条目所以我似乎想在 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/

相关文章:

linq - 如何递归地按属性批量收集

c# - 当多个用户可以编辑相同数据时首选数据库/webapp 并发设计

c# - 从 LINQ 数据库查询中的整数返回枚举名称

c# - 投影匿名类型时 OrderBy 选择器失败

c# - 在 .net 中重置 IIS 后经过身份验证的用户

c# - Linq 表达式从范围引用但未定义

c# - AsEnumerable().Take

c# - LINQ C# 从字符串中选择字符

c# - 如何在 IIS 7 中配置 Http 处理程序?

c# - 在 SqlDbType 枚举中缺少地理数据类型背后是否有逻辑推理