【问题标题】:Filtering in a group using Linq and EF and SQL Server使用 Linq 和 EF 和 SQL Server 在组中过滤
【发布时间】:2018-01-29 20:09:46
【问题描述】:

我有一个这样的 Linq 查询,它会提取表中的所有重复项..

            var duplicates = entities.TableName.GroupBy(item => new { item.Field1, item.Field2 })
                            .Where(g => g.Count() > 1)
                            .SelectMany(g => g)
                            .ToList();

如何过滤表中的另一列,即日期时间类型的 Field3,并希望所有在昨天(即今天 -1)和明天(即今天 + 1)之间具有日期时间的行..

因此,实际上,我想要所有具有 Field1 和 Field2 共同的重复项,并且在其中,我想要所有具有 Field3 类型为 datetime 并且一次在昨天和明天落下的记录

谢谢

【问题讨论】:

    标签: sql-server entity-framework linq


    【解决方案1】:

    在 GroupBy 子句前添加 Where 子句:

    entities.TableName.Where(x => x > DateTime.Now.AddDays(-1)).GroupBy(...)
    

    由于 EF 对某些类型的 DateTime 函数存在一些问题,我编写 Where 子句的方式可能存在问题,在这种情况下,您可能需要使用 Entity Functions 库中的函数。

    【讨论】:

    • 让我回复你。在某事中间..欢呼
    猜你喜欢
    • 2012-06-21
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 2014-04-02
    • 1970-01-01
    相关资源
    最近更新 更多