// Foo is a list of object arrays. The number of items // each array is non-specific. // (In this example there is only 3 items, but there could be 100) var foo = new List(); // I add some items to the list. foo.Add(new object[] { 1, "Something", true }); foo.Add(new object[] { 1, "Some Other", false }); foo.Add(new object[] { 2, "Something", false }); foo.Add(new object[] { 2, "Something", false }); // This will get me a distinct list from the array list... // but it requires I know how many items are in each array. List bar = foo.Select(x => new { X1 = x[0], X2 = x[1], X3 = x[2] }) .Distinct() .Select(x => new object[] { x.X1, x.X2, x.X3 }) .ToList(); List bar = ?? /// < -- How can I rewrite the // previous line so that // my code works with n array items?
如果有帮助,我会知道运行时有多少项?
如果在LINQ中不可能,那么有人可以建议我可以用来实现预期结果的方法吗?
如果我理解你,你可以尝试这样的事情:
class Comparer : IEqualityComparer
请尝试使用以下代码
List bar = foo.GroupBy(x => new { X1 = x[0], X2 = x[1], X3 = x[2] }).Select(g => new object[] { g.Key.X1, g.Key.X2, g.Key.X3, g.Count() }).ToList();