c# - C#Linq结合2个字典

原文 标签 c# linq

我有两个Dictionary<string, Item>Item具有一个公共属性int Level

我想结合这两个字典,其中键是唯一的,并且我希望能够在两个字典上指定级别。

就像是

dictionary 1 = all items that level < 10
dictionary 2 = all items level < 20

combine dictionary 2 with 1 (where Value.Level < 10)
if the Key is unique and the Value.Level < 20

I can easily do this with foreach loops. I can also do this with multiple linq queries. However i can't seem to figure out how to make this one single linq query.

Edit- Per your request John here is the code with foreach

Dictionary<string, Dictionary<string, Item>> itemDictionary = new Dictionary<string, Dictionary<string, Item>>();

Dictionary<string, Item> items = new Dictionary<string,Item>();

            if (itemDictionary.ContainsKey(comboBox.Text))
            {
                foreach (KeyValuePair<string, Item> kvp in itemDictionary[comboBox.Text])
                {
                    if (!items.ContainsKey(kvp.Key) && kvp.Value.Level <= int.Parse(textBox.Text))
                        items.Add(kvp.Key, kvp.Value);
                }
            }

            if (itemDictionary.ContainsKey(comboBox1.Text))
            {
                foreach (KeyValuePair<string, Spell> kvp in itemDictionary[comboBox1.Text])
                {
                    if (!items.ContainsKey(kvp.Key) && kvp.Value.Level <= int.Parse(textBox1.Text))
                        items.Add(kvp.Key, kvp.Value);
                }
            }
            var query = from s in items
                        orderby s.Value.Level
                        select s;

            foreach (var i in query)
               listBox.Items.Add(string.Format("{0} {1}", i.Value.Level, i.Key));

最佳答案

好的,该代码可以清楚地说明您要完成什么。因此,最终结果应为字典,其中包含满足指定级别的两个字典中的项目。如果两个词典中都存在一个项目,则首选第一个词典中的项目。尽管有可能在单个Linq查询中完成此操作,但最终您将需要重复一些工作。这是我想出的,如果您想轻松尝试,它可以在LinqPad中运行。

var itemsOne = new[] {
    new { Name = "A", Level = 1 },
    new { Name = "B", Level = 2 },
    new { Name = "C", Level = 3 },
    new { Name = "D", Level = 4 }
}.ToDictionary(i => i.Name, i => i);

var itemsTwo = new[] {
    new { Name = "C", Level = 10 },
    new { Name = "D", Level = 20 },
    new { Name = "E", Level = 30 },
    new { Name = "F", Level = 40 }
}.ToDictionary(i => i.Name, i => i);

var itemsOneLevel = 3;
var itemsTwoLevel = 30;

var validFromItemsOne = (from item in itemsOne
                         where item.Value.Level <= itemsOneLevel
                         select item).ToDictionary(i => i.Key, i => i.Value);

var validFromItemsTwo = from item in itemsTwo
                        where item.Value.Level <= itemsTwoLevel
                            && !validFromItemsOne.ContainsKey(item.Key)
                        select item;

var items = validFromItemsOne
    .Concat(validFromItemsTwo)
    .ToDictionary(i => i.Key, i => i.Value);

相关文章:

c# - 将SQL Server json属性与代码优先EF结合使用

c# - 流利的NHibernate FluentMappings.AddFromAssemblyOf <>问题

c# - 在泛型方法中创建Expression <Func <T,bool >>时会添加不必要的Convert

c# - 两种错误中均出现匿名类型

c# - 比较两个自定义LIST对象

c# - 从C#包装C ++ DLL的问题

c# - 帮助优化整数数学

c# - 文件上载控件无法返回正确的内容。附加了额外的字符

c# - 获取最大行(Linq,NHibernate)

database - 还使用where子句时如何排序