C# Linq列出任何can';t通用列表的句柄值命令<;列表<;T>&燃气轮机;
我得到这个错误C# Linq列出任何can';t通用列表的句柄值命令<;列表<;T>&燃气轮机;,c#,linq,list,generics,any,C#,Linq,List,Generics,Any,我得到这个错误 'T' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'T' could be found (are you missing a using directive or an assembly reference?) 正在尝试运行此代码 public static List<T> FindCommo
'T' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'T' could be found (are you missing a using directive or an assembly reference?)
正在尝试运行此代码
public static List<T> FindCommon<T>(List<List<T>> lists)
{
var x = from list in lists
from option in list
where lists.All(l => l.Any(o => o.Value == option.Value))
orderby option.Value
select option;
return null;
}
公共静态列表FindCommon(列表列表)
{
var x=列表中的列表
从列表中的选项
其中列出了.All(l=>l.Any(o=>o.Value==option.Value))
orderby选项。值
选择选项;
返回null;
}
测试代码
List<List<uint>> Patterns = new List<List<uint>>();
Patterns.Add(new List<uint>() { 1, 2, 3 });
Patterns.Add(new List<uint>() { 2, 3, 4 });
Patterns.Add(new List<uint>() { 2, 3, 4 });
Patterns.Add(new List<uint>() { 1, 2, 3 });
Patterns.Add(new List<uint>() { 5, 5, 5 });
List<uint> finalOffsets = FindCommon(Patterns);
List Patterns=newlist();
Add(newlist(){1,2,3});
Add(newlist(){2,3,4});
Add(newlist(){2,3,4});
Add(newlist(){1,2,3});
Add(newlist(){5,5,5});
列出FinalOffset=FindCommon(模式);
你也应该回来
1,2,3
或
2,3,4
可能是1,2,3
注意:该
返回空值
是因为我不知道x将返回什么,我需要它作为一个列表。要使代码编译,请删除.Value
,并使用Equals
方法而不是=
。然而,这仍然不能给你想要的(就我理解你的目标而言)
根据我对您尝试执行的操作的理解,您希望找到主列表中重复次数最多的列表。以下是您如何做到这一点:
首先,定义一个知道如何比较列表的比较器:
public class ListEqualityComparer<T> : IEqualityComparer<List<T>>
{
public bool Equals(List<T> x, List<T> y)
{
return x.SequenceEqual(y);
}
public int GetHashCode(List<T> obj)
{
//This works. But you might want to have a
//better way for calculating the hash code
return obj.Sum(x => x.GetHashCode());
}
}
公共类ListQualityComparer:IEqualityComparer
{
公共布尔等于(列表x、列表y)
{
返回x.x(y);
}
public int GetHashCode(列表对象)
{
//这是可行的,但你可能想要一个
//计算散列码的更好方法
返回obj.Sum(x=>x.GetHashCode());
}
}
然后你可以这样使用它:
public static List<T> FindCommon<T>(List<List<T>> lists)
{
return lists.GroupBy(x => x, new ListEqualityComparer<T>())
.OrderByDescending(g => g.Count())
.Select(g => g.Key)
.FirstOrDefault();
}
公共静态列表FindCommon(列表列表)
{
return lists.GroupBy(x=>x,new listQualityComparer())
.OrderByDescending(g=>g.Count())
.选择(g=>g.Key)
.FirstOrDefault();
}
以下是我自己解决问题的方法
public static List<T> FindCommon<T>(List<List<T>> lists)
{
List<uint> Counts = new List<uint>();
List<List<T>> Matches = new List<List<T>>();
bool Found = false;
foreach (List<T> list in lists)
{
Found = false;
for (int i = 0; i < Counts.Count; i++)
{
if (Matches[i].Count == list.Count)
{
for (int j = 0; j < list.Count; j++)
{
//they not equals
if ((dynamic)Matches[i][j] != (dynamic)list[j])
goto next_loop;
//fully equal, increase count for repeated match found.
if (j == list.Count - 1)
{
Counts[i]++;
Found = true;
break;
}
}
}
next_loop:
if (Found) break;
continue;
}
if (!Found)
{
Counts.Add(1);
Matches.Add(list);
}
}
return Matches[Counts.IndexOf(Counts.Max())];
}
公共静态列表FindCommon(列表列表)
{
列表计数=新列表();
列表匹配项=新列表();
bool-Found=false;
foreach(列表中的列表)
{
发现=错误;
for(int i=0;i
删除.Value
。您正在将T
与T
进行比较。您没有任何约束,我不知道您希望beYes删除时使用Value
什么。Value
gives`==`不能应用于't'类型的操作数,而't'`Value不是t的成员。如果您对该类型有所了解,则应使用公共静态列表FindCommon(列表列表)其中T:Foo
尝试使用Equals
而不是=
where列表。All(l=>l.Any(o=>o.Value==option.Value))有点奇怪。因为option是列表的成员,所以o也是。因此,列表的所有成员必须至少有一个元素,其中光标等于该值。这将返回0个计数列表back,因为列表没有任何共同点。最后一个列表包含所有5s,前4个列表没有5s。哦,我想在所有列表中找出哪个列表是所有其他列表中最常见的,占主导地位的列表,我列出的示例有两个主要列表,我想它应该选择第一个我不知道的列表。所以你想找到主列表中重复次数最多的列表吗?如果您有一个包含{1,2,3}的列表和另一个包含{3,2,1}的列表,它们是否被视为相等?@SSpoke,是的。但当两个对象给出相同的哈希代码时,将调用Equals
来确定相等性。因此,更好的哈希代码计算将为您提供更好的性能。但正确性已经得到保证。