【问题标题】:Compare Object vs Dictionary<int,List<int>> considering performance c#比较 Object 与 Dictionary<int,List<int>> 考虑性能 c#
【发布时间】:2020-06-12 17:54:49
【问题描述】:
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp5
{
    class Validator
    {
        static void Main()
        {
            var metaValues = new List<Meta>
                    {
                        new Meta(4, 15, true),
                        new Meta(5, 20, false)
                    };

            var requestDict = new Dictionary<int, List<int>>
                    {
                      {4, new List<int>{15,20} },// error not exist 
                       {5, new List<int>{25} }, // error its false 
                       {6, new List<int>{30} }  // error not exist
                    };

            var matchedIds = new List<int>();

            if (metaValues.Any())
            {
                foreach (var ob in metaValues)
                {
                    if (requestDict.ContainsKey(ob.Id))
                    {
                        matchedIds.Add(ob.Id);
                        var valuesDict = requestDict[ob.Id];

                    //here i cant get all the values and its Active of meta.Id 

                    }
                }
            }

            foreach (var key in requestDict.Keys)
            {
                if (!matchedIds.Contains(key))
                    Console.WriteLine("Invalid");
            }
        }
    }

    public class Meta
    {
        public int Id { get; private set; }
        public int Value { get; private set; }
        public bool IsActive { get; private set; }

        public Meta(int id, int value, bool isActive)
        {
            Id = id;
            Value = value;
            IsActive = isActive;
        }


    }
}

使用对象迭代字典会导致性能问题,因为每次字典键都必须在对象列表中进行迭代,所以我试图在以下条件下获取对象并在字典中查找

  1. 字典键中不存在 meta.Id 时无效
  2. 当 meta.Value 之一在字典值列表中不存在时无效
  3. 当 meta.Id 和 meta.value 与字典匹配但 meta.isactive 为 false 时无效

【问题讨论】:

  • 请修正缩进,这样就没有那么多水平滚动了!
  • 为什么要创建一个与内置类型同名的类?它增加了不必要的混乱(在我看来,无论如何)。
  • 发布的代码不会编译。请使用实际代码更新问题。
  • “对象属性”是什么意思?请解释您的代码的目的,即。你做什么。
  • 请注意,objectObject 在上面的代码中是不同的。

标签: c# dictionary object


【解决方案1】:

我可能不应该费心回答,因为:

  1. 代码比较乱
  2. 无法编译
  3. 问题很不清楚

但是,出于某种原因,我觉得我有点了解您想要做的事情并想提供一些帮助。

首先,我们不要用与内置类型 (System.Object) 相同的名称来命名一个类。也许Item 足够通用?此外,您似乎通过调用一个不存在的构造函数来实例化此类的实例,所以让我们也添加该构造函数:

public class Item
{
    public int Id { get; }
    public int Value { get; }
    public bool IsActive { get; }

    public Item(int id, int value, bool isActive)
    {
        Id = id;
        Value = value;
        IsActive = isActive;
    }
}

现在我们可以通过调用构造函数来创建 Item 对象列表:

var items = new List<Item>
{
    new Item(4, 15, true),
    new Item(5, 20, false)
};

似乎您正在创建一个字典,其中包含一个映射到Item.Idint 类型的Key,以及一个映射到List&lt;int&gt; 类型的Value Item.Value (尽管Item.Value 是单个int)。您发布的代码中的一个问题是您尝试添加两个具有相同Key4 的项目,这对于Dictionary 是不合法的 - 所有键都必须是唯一的。为了解决这个问题,我使用了唯一键:

var requests = new Dictionary<int, List<int>>
{
    {4, new List<int> {15}},
    {5, new List<int> {20}},
    {6, new List<int> {25}},
    {7, new List<int> {30}}
};

接下来,您似乎正在尝试创建一个数字 List&lt;int&gt;,表示作为字典键存在的 Item.Id 值。这可以通过System.Linq 扩展方法来完成:

var matchedIds = items
    .Where(item => requests.ContainsKey(item.Id))
    .ToList();

最后,不清楚你想用这个列表做什么,但是如果字典中不存在Item.Id,或者Item.Id 存在但@987654344,你似乎想要做一些事情@ 不在列表中,或者项目确实 存在,但Item.IsActive 的值是false,或者这些属性的其他组合。

获取这些物品的方法如下:

var matchedIds = items
    .Where(item => requests.ContainsKey(item.Id))
    .ToList();

var matchedIdsAndValues = matchedIds
    .Where(item => requests[item.Id].Contains(item.Value))
    .ToList();

var matchedIdsMissingValue = matchedIds
    .Where(item => !requests[item.Id].Contains(item.Value))
    .ToList();

var unmatchedIds = items
    .Where(item => !requests.ContainsKey(item.Id))
    .ToList();

var matchedIdAndValueButNotActive = matchedIdsAndValues
    .Where(item => !item.IsActive)
    .ToList();

希望这会有所帮助!

【讨论】:

  • 感谢您的回复。但是我在 linq 中也遇到了同样的问题。这里matchedIdsMissingValue我想在请求有行而不是matchedIds时返回。你能帮我解决这个问题,因为我还在努力解决这个问题吗?提前感谢@Rufus L
猜你喜欢
  • 2018-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-15
相关资源
最近更新 更多