如何使用lambda或linq从整数列表中的对象列表中收集多个int属性?

Gia*_*vas 1 c# linq lambda

我试过这个C#代码:

//items is the list of objects that contain the property a (int) and b(int)
var listOfInts= items.Select(p => new {p.a, p.b}).Distinct().
                            OrderByDescending(p => p.a).ToList();               

//the result is a list of a' wew {p.a, p.b} AND not a list of integers.
Run Code Online (Sandbox Code Playgroud)

我只想将所有对象的这两个属性添加到一个简单的整数列表中,即linq或lambda表达式.

提前致谢!

Mar*_*zek 7

你可以使用SelectMany:

var listOfInts = items.SelectMany(p => new[] { p.a, p.b })
                      .Distinct()
                      .OrderByDescending(x => x)
                      .ToList();
Run Code Online (Sandbox Code Playgroud)