[英]How to check an object of type List<Dictionary<string, string>> to set one value based off another value in the dictionary?
I have an object called reportData, which is holding the data for a report.我有一个名为 reportData 的对象,它保存报表的数据。 This object is of type
List<Dictionary<string, string>>
I need to add logic to manipulate reportData that when the value of the key "Type" is Withdrawal the value of the key "TransAmount" should have a minus sign before it.该对象的类型为
List<Dictionary<string, string>>
我需要添加逻辑来操作 reportData,当键“Type”的值是 Withdrawal 时,键“TransAmount”的值前面应该有一个减号。 I was thinking I can accomplish this with some linq but I am not having any success.我以为我可以用一些 linq 来完成这个,但我没有任何成功。
This is what I tried so far...这是我到目前为止尝试过的...
foreach (var kvp in reportData.SelectMany(m => m).Where(x => x.Value != null))
{
if (kvp.Value.Equals("Deposit"))
(
//Over here I need to set the value of the key "TransAmount" to have a minus sign before it, but I'm not sure how to go about it
)
}
Over here is a snapshot of the data that is being held in reportData.这里是 reportData 中保存的数据的快照。 The showcased item in the list is of Type "Withdrawal", My code needs to deal with items in the list that are of Type "Deposit"
列表中展示的项目类型为“提款”,我的代码需要处理列表中类型为“存款”的项目
https://gyazo.com/a5183aa404e51672712d680dcd8ad6af https://gyazo.com/a5183aa404e51672712d680dcd8ad6af
How about something like this ?这样的事情怎么样?
foreach (var dict in reportData)
{
var keys = new List<string>(dict.Keys);
foreach (string key in keys)
{
if (key == "Type")
{
if (dict[key] == "Deposit")
{
dict["TransAmount"] = "-" +dict["TransAmount"] ;
}
}
}
}
Try this https://dotnetfiddle.net/Ii0MR7试试这个https://dotnetfiddle.net/Ii0MR7
We only need one loop and one operation.我们只需要一个循环和一个操作。 We take dictionaries one by one, in each dictionary we are looking for the specific key "Type" while at the same time trying to get its value to variable called
type
(that's precisely what TryGetValue
does).我们一个一个地获取字典,在每个字典中我们都在寻找特定的键“Type”,同时尝试将其值获取到称为
type
变量(这正是TryGetValue
所做的)。 It also returns true
in case when element exists.如果元素存在,它也会返回
true
。 At this point we only need to make sure that the value of it is the one we're looking for.在这一点上,我们只需要确保它的值是我们正在寻找的值。 If yes - we get in to the code block and modify another value.
如果是 - 我们进入代码块并修改另一个值。 If you're not familiar with
$
string interpolation please check this article.如果您不熟悉
$
字符串插值,请查看这篇文章。
foreach (var dict in reportData)
{
if (dict.TryGetValue("Type", out var type)
&& type == "Withdrawal"
&& dict.TryGetValue("TransAmount", out var amt)
&& !string.IsNullOrEmpty(amt))
{
dict["TransAmount"] = $"-{dict["TransAmount"]}";
}
}
And yes you can do it with LINQ but it is not recommended, a good use-case for LINQ
is data querying and for manipulating data it is better to use good old loops, nevertheless here is the code:是的,你可以使用LINQ做,但不建议这样做,一个良好的用例的
LINQ
是数据查询和操纵数据,最好是用好老的循环,不过这里是代码:
reportData = reportData.Select(d =>
{
if (d.TryGetValue("Type", out var type) && type == "Withdrawal")
{
d["TransAmount"] = $"-{d["TransAmount"]}";
}
return d;
}.ToList(); // this will create a new instance of List<Dictionary>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.