【问题标题】:Get List of Dates that falls with a month from a table that holds Transactions从包含事务的表中获取一个月内的日期列表
【发布时间】:2020-02-19 17:23:31
【问题描述】:

大家好,又来了

好的,快点,我有一个表格,可以在交易日期旁边存储交易。 正在编写一份报告以检索过去 6 个月内每个月的所有交易总额。

目前我正在使用它来检索一个数组,该数组包含当前月份的最后六个月。

//this guy gets the date of each month till the last 6 month
 DateTime[] lastSixMonths = Enumerable.Range(0, 6).Select(i => DateTime.Now.AddMonths(-i)).ToArray();

//here am loopin through each month and calling a service passing the month in loop as parameters
            foreach (var month in lastSixMonths)
            {
                var transAmountforthemonthinLoop = await _transactionsRepository.GetTransactionsAmountSumWithinAMonth(month);
                months.Add(month);
                monthSum.Add(transAmountforthemonthinLoop);
            }



//this is the service i am calling above
public async Task<double> GetTransactionsAmountSumWithinAMonth(DateTime month)
        {
            var monthString = month.ToString("MM/yyyy");
            var trans = await _context.Transactions.Where(c => c.DateCreated.Equals(month) && c.ResponseCode == "00").Select(c => c.Amount).ToListAsync();
            var sum = trans.Sum();
            return sum;
        }

this isnt working as expected

例如,我想要的是。


if the first element in loop is say 2020/02/19

i want to go to my transactions table and sum all the transactions that has 2020/02 ie february as its months. and a responseCode of 00 which is successfl payment

Please any assistance will be appreciated.
``
this 


【问题讨论】:

  • Transactions表中DateCreated的格式是什么?它只是 MM/yyyy 格式还是其他格式?你能发布示例数据吗?

标签: c# .net linq asp.net-core


【解决方案1】:

如果循环中的第一个元素是 2020/02/19

我想转到我的交易表并汇总所有以 2020/02 即 2 月为月份的交易。并且 responseCode 为 00 表示付款成功

EF3 发生了重大变化。从 3.0 开始,EF Core 仅允许在客户端评估顶级投影(查询中的最后一个 Select() 调用)中的表达式。当查询的任何其他部分中的表达式无法转换为 SQL 或参数时,将引发异常。

要在客户端评估谓词条件,请尝试在GetTransactionsAmountSumWithinAMonth 中使用以下代码

var trans =   _context.Transactions.AsNoTracking()
                      .AsEnumerable()
                      .Where(c => (c.DateCreated.ToString("MM/yyyy") == monthString)&& (c.ResponseCode == "00"))
                      .Select(c => c.Amount)
                      .ToList();

参考https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/#restricted-client-evaluation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多