【问题标题】:linq subtracts number in a list [duplicate]linq减去列表中的数字[重复]
【发布时间】:2020-07-16 12:50:17
【问题描述】:

如何在查询列表中减去数字?

var q = lists.Select(v => new LegalSuitReport
{
    CargoQtystr= v.Claim.ClaimBLs.Select(qq => qq.JobBL.CargoQty).ToList(),
    RecipetedQtystr = v.Claim.ClaimBLs.Select(qq => qq.JobBL.LandedQty).ToList()
    diff= v.Claim.ClaimBLs.Select(qq => qq.JobBL.CargoQtystr) - v.Claim.ClaimBLs.Select(qq => qq.JobBL.LandedQty)

}).ToList().Select(qs => new LegalSuitReport()
{
    CargoQty= string.Join(",", qs.CargoQtystr),
    RecipetedQty = string.Join(",", qs.RecipetedQtystr)
    diff = string.Join(",", qs.RecipetedQtystr)

}).ToList();

我想在这些cargoqty 和receiptedqty 中减去具有相同索引的数字,并且差异之间存在差异

List<decimal> cargoqty= new List<decimal>{500,100000,150};
List<decimal> RecipetedQtystr = new List<decimal>{5,90000,15};
List<decimal> diff= new List<decimal>{495,10000,135};

【问题讨论】:

  • 你能分享一下,示例输入和例外输出。
  • 中间不用ToList()
  • 您的代码没有显示任何“减法”尝试。从样本来看,Zip() 会有所帮助
  • 中间不需要使用ToList() =>请详细说明?

标签: c# asp.net-mvc linq


【解决方案1】:

从样本来看,Zip() 似乎会有所帮助:

var diff=cargoqty.Zip(RecipetedQtystr,(q1,q2)=>q1-q2).ToList();

Zip 成对组合两个序列。这个特殊的重载在返回结果之前将一个函数应用于对

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    相关资源
    最近更新 更多