Skipwhile in linq

Hi All
Need some help to use skipwhile in linq as I know it’s use to skip the data until condition get true for example I have input no i.e 1,2,3,4,5,6 when I use skipwhile if no <3 so it will skip 1 and 2 and my result is 3-6 but one question is creating confussion how answer is 10

Hi @Aleem_Khan

dt.AsEnumerable.SkipWhile(Function(x) x("item").ToString.Equals("mango"))(0)("Quantity").ToString

Below is the explanation for the LINQ:

  • dt.AsEnumerable: Converts the DataTable (dt) into an enumerable collection of rows, so LINQ can be used on it.
  • .SkipWhile(Function(x) x("item").ToString.Equals("mango")): This skips rows as long as the "item" column is "mango". As soon as it finds a row where "item" is not "mango", it stops skipping.
  • (0): After skipping the "mango" rows, it picks the first row that remains.
  • ("Quantity"): From that row, it gets the value in the "Quantity" column.
  • .ToString: Converts that value to a string.

So, the answer is 10

Hope it helps!!

Hi @Aleem_Khan

Basically in this perspective Skipwhile skips the datarows were row(“item”) is mango
So the output will be whole datatable (iEnumerable of datarows) itself until the row is having mango in items.
So (0) is nothing but index accessing the 1st row, same as example accessing the 1st element in an array
So the answer is 10

Hope this helps

So.my questions here it will stop skiping at the point of mango found so my data is mango ,kiwi and pear is my understanding correct if not please help

I have checked this explains in gpt but figure out but need to know both scenarios how it’s working

Hi @Aleem_Khan

It will stop skipping according to giving condition if it’s mango/ pear kiwi and still 0 is given in the LINQ it will be taking the Quantity of the first row only.

Regards

1 Like

@Aleem_Khan
No it it will take all the datarows until the mango is reached, when the condition is true it will skip the rest of the rows

Check in this video

So my understanding is correct this is tricky question where zero is first row index even it’s skip any but always find value from given index am I correct?

Yes @Aleem_Khan

Your understanding is right.

Regards