Select only first 2 items in an iEnumerable

Hi, I have an iEnumerable collection, how do I select only the first 2 items without looping? i’ve seen the code somewhere it looks like next() and skip() but I’m not exactly sure how it works


Try this:

newdt = myDataTable.AsEnumerable().Take(2).CopyToDataTable

so if I want to extract it as an Array, is this the code?

arr = enum.Take(2).ToArray?

1 Like

yes @DEATHFISH that should work…

ok now the error that I’m facing is that enum is type iEnumerable<Object> and I want to produce arr which is of type String[]. What is the most efficient way of doing this?


Please check attached workflow for your reference.

Flowchart.xaml (8.1 KB)

what kind of items you have in your enum collection…

the enum is from Excel Read Column activity

try this…

(From x in Enumobj.Take(2) select y = x.ToString).ToArray