[英]How to retrieve the specific indexes from array?
我有這個數組:
Point[] arr = samples.pointsArray;
我使用此行來檢索所有滿足條件的元素:
var maxXCol = arr.Where( p => maxX.X == p.X );
任何想法如何修改上面的行,以僅獲取這些元素的索引?
先感謝您!
編輯
使用同時包含索引和對象的Select
版本,並在其中包含對象和索引的情況下創建匿名對象。 它看起來像這樣:
someEnumerable.Select((obj, idx) => new {Item = obj, Index = idx})
在使用Where
之前,您需要執行此操作,以使原始索引在過濾操作后保持不變。
在以下操作中,您可以像這樣使用該項目:
x => x.Item
索引如下:
x => x.Index
您可以使用Select
重載來獲取索引,然后將該索引與原始行一起投影。 然后僅獲取結果集合的索引。
var maxXCol = arr
.Select((p, index) => new { Item = p, Index = index })
.Where(p => maxX.X == p.Item.X)
.Select(x => x.Index);
var maxXCol = arr.Select((p, inx) => new { p,inx})
.Where(y => maxX.X == y.p.X)
.Select(z => z.inx);
您可以先使用匿名類型的索引選擇值,然后再根據條件過濾它,然后選擇索引。
var result = arr.Select((g, index) => new { g, index })
.Where(r => maxX.X == r.X)
.Select(t => t.index);
嘗試這個:
arr.Select((e,i)=>new{index=i, value=e}).Where(ei=>ei.value.X==maxX.X).Select(ei=>ei.index);
var maxXCol = arr
.Select((a, b) => new { b, a })
.Where(p => maxX.X == p.a.X)
.Select(i=>i.b);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.