如何使用LINQ從某些數組中獲得有序的元素對?例如, 我有:Descarte的數組元素的方差
int[] d = { 1, 2, 3 };
我需要:
{{1,1},{1,2},....,{3,3}}
我試過LINQ查詢,但它返回
{{1,1},{2,2},{3,3},{1,1},{2,2},{ 3,3},{1,1},{2,2},{3, 3}}
var pairs = d.SelectMany(a => d.Select(b => new[] { a, b }));
請幫我找到我的錯誤。
您所提供的代碼可以產生你自稱想要的序列,沒有序列你說它。 – Servy
您是否在尋找https://en.wikipedia.org/wiki/Cartesian_product(其中涵蓋了很多問題,包括http://stackoverflow.com/questions/3093622/generating-all-possible-combinations)?由於@Serv代碼在帖子中似乎產生了你想要的結果... –