Linq List任何无法处理泛型List <list <t>&gt;的Value命令。

时间:2016-02-02 20:58:48

标签: 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> 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会返回什么,我需要它作为列表。

2 个答案:

答案 0 :(得分:3)

要编译代码,请删除.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());
    }
}

然后你可以像这样使用它:

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();
}

答案 1 :(得分:0)

以下是我自己解决问题的方法。

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())];
}