4 回答

TA貢獻1735條經驗 獲得超5個贊
您可以為此使用匿名對象..
var items = result.Select(p => new { ValueA = p.GetValue(0), ValueB = p.GetValue(1) });
然后訪問每個項目
foreach(var item in items)
{
var valueA = item.ValueA;
var valueB = item.ValueB;
}

TA貢獻1810條經驗 獲得超4個贊
您可以使用Take
擴展方法:
items.Take(x);
這將返回x
集合的第一個項目。
如果你想跳過某些元素,可以使用Skip(x)
before 調用Take
。這兩種方法經常用于分頁。

TA貢獻1818條經驗 獲得超8個贊
private class Foo
{
? ? public int Item1;
? ? public int Item2;
? ? public int Item3;
}
static void Main(string[] args)
{
? ? List<Foo> foos = new List<Foo>?
? ? ? ? ? ? ? ? ? ? ? ? ? ?{?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new Foo() { Item1 = 1, Item2 = 2, Item3 = 3 },
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new Foo() { Item1 = 4, Item2 = 5, Item3 = 6 },
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new Foo() { Item1 = 7, Item2 = 8, Item3 = 9 }
? ? ? ? ? ? ? ? ? ? ? ? ? ?};
? ? // Create a list of lists where each list has three elements corresponding to?
? ? // the values stored in Item1, Item2, and Item3.? Then use SelectMany
? ? // to flatten the list of lists.
? ? var items = foos.Select(f => new List<int>() { f.Item1, f.Item2, f.Item3 }).SelectMany(item => item).Distinct();
? ? foreach (int item in items)
? ? ? ? Console.WriteLine(item.ToString());
? ? Console.ReadLine();
}

TA貢獻1812條經驗 獲得超5個贊
如果你想要不同然后2,那么,
result.Select(p => p).Distinct().Take(2);
如果只有2的話,
result.Take(2);
- 4 回答
- 0 關注
- 149 瀏覽
添加回答
舉報