C# 查询值
双字典中的声明和条目按如下所示创建:C# 查询值,c#,linq,C#,Linq,双字典中的声明和条目按如下所示创建: 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 L
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]);
}
我希望形成一个linq查询,以获取字典two\u Dict
中具有相同列表项的键。任何帮助都将不胜感激 这个怎么样
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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
Dictionary two_Dict=新字典();
var keyIndex=2个Dict.KEYSE.AsEnumerable()
.Select((x,i)=>new{index=i,value=x})
.ToList();
var结果=(来自keyIndex中的key1
从键索引中的键2
其中key1.index>key2.index
其中A相等(两个数字[key1.value],两个数字[key2.value])
选择new{key1=key1.value,key2=key2.value}).ToList();
}
静态布尔值相等(列表x、列表y)
{
//相同的列表或两者都为空
如果(x==y)
{
返回true;
}
//一个为空(但另一个不为空)
如果(x==null | | y==null)
{
返回false;
}
//计数不同;它们不相等
如果(x.Count!=y.Count)
{
返回false;
}
x、 排序();
y、 排序();
对于(int i=0;i
所有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 };
可以对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);
}
}
}
谢谢我试图通过选择新的{k1,k2,dict[k1]}将dict[k1]的条目添加到“res”中。它给我一个错误“无效的匿名类型成员声明器”。这里有什么问题?试试新的{k1,k2,dictval=dict[k1]}
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);