【发布时间】:2016-05-11 20:38:50
【问题描述】:
我收到此错误
'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;
}
测试代码
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);
应该返回 1,2,3 要么 2,3,4
可能是 1,2,3
注意:return null; 是因为我不知道 x 会返回什么,所以我需要它作为一个列表。
【问题讨论】:
-
删除
.Value。您将T与T进行比较。你没有限制,我不知道你对Value的期望是什么 -
是的,删除
.Value后,`'==' 不能应用于 'T' 和 'T' 类型的操作数` -
Value不是 T 的成员。如果您对类型有所了解,您应该使用public static List<T> FindCommon<T>(List<List<T>> lists) where T : Foo -
尝试使用
Equals而不是== -
where lists.All(l => l.Any(o => o.Value == option.Value))有点奇怪。因为 option 是列表的成员,所以 o 也是。因此,列表的所有成员必须至少有一个元素的光标等于该值。