Query for values

本文关键字:values for Query | 更新日期: 2023-09-27 17:53:52

创建two_Dict字典中的声明和条目:

Dictionary<string, List<string>>two_Dict = new Dictionary<string, List<string>>();
List<string> list; 
if (!two_Dict.TryGetValue(d.ToString(), out list))
{
    two_Dict.Add( d.ToString(), list = new List<string>());
    list.Add(possibility_cell_list[0]);
    list.Add(possibility_cell_list[1]);
}

two_Dict中的样例条目:

two_Dict["5"] Count = 2 [0]: "A2"  [1]: "D2"
two_Dict["6"] Count = 2 [0]: "A2"  [1]: "D2"

我希望形成一个linq查询,以获得在字典two_Dict中具有相同列表条目的键。任何帮助都将不胜感激。

Query for values

您可以使用一个相当简单的表达式linq:

var keys = from kvp1 in two_dict
           where two_dict.Any(kvp2 => kvp2.Key != kvp1.Key
               && kvp2.Value.SequenceEqual(kvp1.Value))
           select kvp1.Key;

然而,这并不能提供最好的性能,因为它将搜索整个字典n次,其中n是字典中条目的数量。

如果您只查看到目前为止已经查看过的项,您可以获得稍好的性能。这样,你平均只遍历一半的字典n次,所以理论上它的速度是原来的两倍。不幸的是,我不认为有一个很好的方法来做这纯粹使用linq。

public static IEnumerable GetDuplicates(IDictionary<string, List<string>> dict)
{
    var previousItems = new List<KeyValuePair<string, List<string>>>(dict.Count);
    var matchedItems = new List<bool>();
    foreach (var kvp in dict)
    {
        var match = previousItems.Select((kvp2, i) => Tuple.Create(kvp2.Key, kvp2.Value, i)).FirstOrDefault(t => kvp.Value.SequenceEqual(t.Item2));
        if (match != null)
        {
            var index = match.Item3;
            if (!matchedItems[index])
            {
                yield return match.Item1;
                matchedItems[index] = true;
            }
            yield return kvp.Key;
        }
        else
        {
            previousItems.Add(kvp);
            matchedItems.Add(false);
        }
    }
}

你可以这样调用这个函数:

var keys = GetDuplicates(two_dict);

所有linq,没有重复的结果,但也要小心,没有空检查:

            var res = from k1 in dict.Keys
                from k2 in dict.Keys
                where string.Compare(k1, k2) == -1 && 
                dict[k1].Count == dict[k2].Count && !dict[k1].Any(x => !dict[k2].Contains(x)) && !dict[k2].Any(x => !dict[k1].Contains(x))
                select new { k1, k2 };

这个怎么样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, List<string>> two_Dict = new Dictionary<string, List<string>>();
            var keyIndex = two_Dict.Keys.AsEnumerable()
                .Select((x, i) => new { index = i, value = x })
                .ToList();
            var result = (from key1 in keyIndex
                          from key2 in keyIndex
                          where key1.index > key2.index 
                          where AreEqual(two_Dict[key1.value],two_Dict[key2.value])
                          select new {key1 = key1.value, key2 = key2.value}).ToList(); 
        }
        static bool AreEqual<T>(List<T> x, List<T> y)
        {
            // same list or both are null
            if (x == y)
            {
                return true;
            }
            // one is null (but not the other)
            if (x == null || y == null)
            {
                return false;
            }
            // count differs; they are not equal
            if (x.Count != y.Count)
            {
                return false;
            }
            x.Sort();
            y.Sort();
            for (int i = 0; i < x.Count; i++)
            {
                if (!x[i].Equals(y[i]))
                {
                    return false;
                }
            }
            return true;
        }
    }
}
​