gpt4 book ai didi

c# - LINQ:处理匿名类型

转载 作者:太空宇宙 更新时间:2023-11-03 17:13:56 24 4
gpt4 key购买 nike

我正在尝试生成以下形式的图表:

User A    User B     Owes      Owed     Net
Sam David $20 $10 $10
Timbo ODP $30 $0 $30

使用以下查询:

        var debt = from user in users
select new {
Username = username,
User = user,
Owes = owedBetween(username, user),
Owed = owedBetween(user, username),
Net = Owes - Owed // doesn't compile
};

问题是最后一行没有编译。有没有办法在查询中设置 Net 值,或者我必须将它初始化为零然后再更改它?

最佳答案

尝试使用 let 关键字:

  var debt = from user in users
let owes = owedBetween(username, user)
let owed = owedBetween(user, username)
select new {
Username = username,
User = user,
Owes = owes,
Owed = owed,
Net = owes - owed
};

关于c# - LINQ:处理匿名类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3900697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com