简体   繁体   English

如何检查 List 类型的对象<Dictionary<string, string> &gt; 根据字典中的另一个值设置一个值?

[英]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.

相关问题 如何修改列表中的值 <Dictionary<string,object> &gt; - How to modify the value in List<Dictionary<string,object>> 排序字典 <string, List<int> &gt;基于价值 - Sorting Dictionary <string, List<int>> based on value 字典&lt;(字符串,字符串,字符串),列表<object> &gt; 如何获取与此类字典的 1 个键匹配的值 - Dictionary<(string, string, string), List<object>> how to get value that match 1 key of this kind of dictionary 在List中搜索值 <Dictionary<string, object> &gt; - Search for a value in List<Dictionary<string, object>> 字典如何 <string, object> 知道值的类型吗? - How does Dictionary<string, object> know the type of the value? 列表<dictionary<string,object> &gt; 引用类型不会更改为新的字典值</dictionary<string,object> - List<Dictionary<string,object>> reference type doesn't change to new dictionary value 如何将我的y.Value投射到列表 <Dictionary<string, object> &gt; - how to cast my y.Value to List<Dictionary<string, object>> 检查字典中是否已经存在值 <string, List<string> &gt; - Check if value already exists in dictionary<string, List<string>> 如何设置 HashSet<string> 作为字典中的值?</string> - How to set a HashSet<string> as a Value in Dictionary? 如何从字典中获得价值<string,object> - How to get value from Dictionary<string,object>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM