我正在尝试生成以下形式的图表:
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/