C#Linq查找符合条件的记录,如果找到该记录,则返回所有相关记录

时间:2018-08-03 16:17:46

标签: c# linq

我的问题:我正在搜索发生在特定日期的帐户交易。如果我找到当天的交易记录,则需要收集该帐户的交易记录并将其捆绑在一起。我当前的解决方案需要2条linq语句才能正确恢复数据。但是我还有其他一些数据库调用,因此我正在尝试减轻负载

我当前的解决方案:首先,我使用linq语句来收集当天发生的所有交易,并且仅返回帐号。然后,我重新运行几乎相同的查询,但是这次使用了我需要的一切,并使用了where子句中第一个查询中的帐号。 我还发现linq不喜欢where子句中使用的匿名类型,因此我将帐号转换为中间的列表。

我的请求:有人可以帮助我找到一种更有效的方法来恢复我需要的数据吗?如果有人可以建议我可以对匿名问题进行​​更改,我将不胜感激。错误:无法创建类型为“匿名类型”的常量值。在这种情况下,仅支持基本类型或枚举类型。

我的代码

public ICollection<ACCOUNT_TRANS> GetTransactionsByDate(DateTime date)
    {
        //Get account transactions that occur on this date
        var results = this.ACCOUNT_TRANS
                          .Select(at => new { at.FK_ACCOUNT, at.COMPLETION_DATE })
                          .Where(at => at.COMPLETION_DATE.Value.Day == date.Day &&
                                        at.COMPLETION_DATE.Value.Month == date.Month &&
                                        at.COMPLETION_DATE.Value.Year == date.Year)
                          .ToList();
        //Extract Account Number and removes the anonymous nature of the data
        var accountNums = results.Select(r => r.FK_ACCOUNT).ToList();

        //Return Transaction history for all changed changed
        var results2 = this.ACCOUNT_TRANS
                           .Include(at => at.ACCOUNT_TABLE1)
                           .Include(at => at.ACCOUNT_TABLE2)
                           .Include(at => at.ACCOUNT_TABLE3)
                           .Include(at => at.ACCOUNT_TABLE4)
                           .Where(at => accountNums.All(r => r == at.FK_ACCOUNT))
                           .ToList();

        return results2;
    }

已解决

问题解决了,因为我尝试做很多事情都扭曲了我的头。代码如下所示。 Linq想要的方式简单而精致:

public ICollection<ACCOUNT_TRANS> GetTransactionsByDate(DateTime date)
    {
        //Return Transaction history for all changed changed
        var results = this.ACCOUNT_TRANS
                           .Include(at => at.ACCOUNT_TABLE1)
                           .Include(at => at.ACCOUNT_TABLE2)
                           .Include(at => at.ACCOUNT_TABLE3)
                           .Include(at => at.ACCOUNT_TABLE4)
                           .Where(at => at.COMPLETION_DATE.Value.Day == date.Day &&
                                        at.COMPLETION_DATE.Value.Month == date.Month &&
                                        at.COMPLETION_DATE.Value.Year == date.Year)
                           .ToList();

        return results;
    }

1 个答案:

答案 0 :(得分:2)

尝试此查询,仅此查询。

    var results2 = this.ACCOUNT_TRANS
                   .Include(at => at.ACCOUNT_TABLE1)
                   .Include(at => at.ACCOUNT_TABLE2)
                   .Include(at => at.ACCOUNT_TABLE3)
                   .Include(at => at.ACCOUNT_TABLE4)
                   .Where(at => this.ACCOUNT_TRANS
                            .Where(a => at.COMPLETION_DATE.Value.Day == date.Day &&
                                a.COMPLETION_DATE.Value.Month == date.Month &&
                                a.COMPLETION_DATE.Value.Year == date.Year)
                            .Select(a => a.FK_ACCOUNT).Contains(at.FK_ACCOUNT))
                   .ToList();
相关问题