简体   繁体   English

当按日期净值归零时,如何从列表中删除

[英]How to remove from a List when total values by date net to zero

I have a C# List of transactions我有一个 C# 交易列表

public class Transaction
{
        public string effectiveDate;
        public string transactionType;
        public double grossAmount;
        public double netAmount;
}

Different transactionTypes are stored in different lists which are populated pulling data from database.不同的事务类型存储在不同的列表中,这些列表从数据库中提取数据。 At the end it spits out a statement of itemised transactions.最后,它会输出一个逐项交易的声明。

Lets say I have a monthly admin fee $1.50 paid on 15th of the month.假设我在每月 15 日支付了每月 1.50 美元的管理费。 In February the fee is waived, and I have a reversal transaction. 2月份免手续费,我有逆向交易。 So now in my list I have所以现在在我的清单中我有

'2020-01-15', 'Admin fee', 1.5, 1.5
'2020-02-15', 'Admin fee', 1.5, 1.5
'2020-02-15', 'Admin fee', -1.5, -1.5
'2020-03-15', 'Admin fee', 1.5, 1.5

How can I remove or skip the Feb admin fees if they nett out to zero?如果 2 月份的管理费净额为零,我该如何取消或跳过这些费用?

In other instances I might have 10 or more transactions of the same type on the same day that get reversed, ie 10 payments received and reversed with same effective date.在其他情况下,我可能在同一天有 10 笔或更多相同类型的交易被撤销,即在相同的生效日期收到和撤销了 10 笔付款。

Is sorting by date, iterating and keeping running total of values until I get to a new date then comparing to zero the easiest/nicest way?是按日期排序、迭代并保持运行总值直到我到达新日期然后与零进行比较是最简单/最好的方法吗?

I know this would be easier to do in the SQL with group by having sum <> 0, but the values are stored in database as chars with $ and comma and leading trailing whitespace etc so I cant go that way.我知道这在带有组的 SQL 中通过 sum <> 0 会更容易做到,但是这些值作为带有 $ 和逗号以及前导尾随空格等的字符存储在数据库中,所以我不能那样做。 For various reasons, not even if I replace/replace/replace/convert the data.由于各种原因,即使我替换/替换/替换/转换数据也不会。

Thanks谢谢

You can try Linq , for given list, say您可以尝试Linq ,对于给定的列表,例如

  var myList = new List<Transaction>() {
    new Transaction() {
      effectiveDate = "2020-01-15",
      transactionType = "Admin fee",
      grossAmount = 1.5,
      netAmount = 1.5},

    new Transaction() {
      effectiveDate = "2020-02-15",
      transactionType = "Admin fee",
      grossAmount = 1.5,
      netAmount = 1.5},

    new Transaction() {
      effectiveDate = "2020-02-15",
      transactionType = "Admin fee",
      grossAmount = -1.5,
      netAmount = -1.5},

    new Transaction() {
      effectiveDate = "2020-03-15",
      transactionType = "Admin fee",
      grossAmount = 1.5,
      netAmount = 1.5},
  };

You can put Linq query:你可以把 Linq 查询:

  1. We GroupBy transactions with the same effectiveDate and transactionType我们GroupBy交易与同effectiveDatetransactionType
  2. Check if Sum of netAmount is not 0检查Sum netAmount是否为 0
  3. Flatten ( SelectMany followed by ToList ) groups back to list展平( SelectMany后跟ToList )组回到列表

Code:代码:

  using System.Linq;

  ...

  myList = myList
    .GroupBy(t => new { t.effectiveDate, t.transactionType })
    .Where(group => group.Sum(t => t.netAmount) != 0)
    .SelectMany(group => group)
    .ToList();

I think, you could use LinQ to group your results by day and type and then calculate the sum of the grouped elements.我认为,您可以使用 LinQ 按天和类型对结果进行分组,然后计算分组元素的总和。 When sum==0 skip...当 sum==0 跳过...

List<Transaction> transactions;
var noUnderZeroFees = transactions.Where(x => x.netAmount > 0).ToList<Transaction>();
//noUnderZeroFees will contains Transaction items with more then Zero netAmount.

Try to search more about LINQ尝试搜索有关 LINQ 的更多信息

You can use Linq:您可以使用 Linq:

List<Transaction> loList = new List<Transaction>();
loList.Add(new Transaction()
{
    effectiveDate = "2020-01-15",
    transactionType = "Admin fee",
    grossAmount = 1.5,
    netAmount = 1.5
});
loList.Add(new Transaction()
{
    effectiveDate = "2020-02-15",
    transactionType = "Admin fee",
    grossAmount = 1.5,
    netAmount = 1.5
});
loList.Add(new Transaction()
{
    effectiveDate = "2020-02-15",
    transactionType = "Admin fee",
    grossAmount = -1.5,
    netAmount = -1.5
});
loList.Add(new Transaction()
{
    effectiveDate = "2020-03-15",
    transactionType = "Admin fee",
    grossAmount = 1.5,
    netAmount = 1.5
});

foreach (var loGTrans in loList.GroupBy(item => new { item.transactionType, item.effectiveDate }).Where(item => item.Sum(t => t.netAmount) != 0))
{
    Console.WriteLine($"Type: {loGTrans.Key.transactionType}, Date: {loGTrans.Key.effectiveDate} -> Gross-Sum: ${loGTrans.Sum(item => item.grossAmount)} Net-Sum:${loGTrans.Sum(item => item.netAmount)}");
}

You first GroupBy() transactionType and then you calculate the Sum() of netAmount foreach group.您首先GroupBy() transactionType,然后计算每个组的 netAmount 的Sum() With the Where() -statement you include only the groups that fit the filter and then you dissolve the groups with SelectMany()使用Where()语句,您只包含适合过滤器的组,然后使用SelectMany()解散这些组

    private IEnumerable<Transaction> FilterNetZero(IEnumerable<Transaction> list)
    {
        return list.GroupBy(s => s.transactionType)
                   .Where(group => group.Sum(s => s.netAmount) != 0)
                   .SelectMany(group => group);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM