查询值

时间:2015-06-07 11:10:10

标签: c# linq

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中具有相同列表条目的键。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

您可以使用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);

答案 1 :(得分:0)

这个怎么样

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;
        }
    }
}
​

答案 2 :(得分:0)

所有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 };