1

I would like to split an array of objects into multiple array that hold no more than 10 elements.

Spent quite a while looking at examples, and the closest I can find is in C#:

Enumerable.Range(0, 100).Select((Value, Index) => new {Value, Index})
    .GroupBy(p => p.Index/10)
    .Select(g => g.Select(p => p.Value).ToList())

This runs and works in LinqPad, so I tried to convert it to VB.Net:

Enumerable.Range(0, 100).Select(Function(Value, Index) New With {Value, Index}) _
    .GroupBy(Function(x) x.Index / 10) _
    .Select(Function(g) g.Select(Function(p) p.Value).ToList())

But this doesn't give the same result, even though it looks like it should.

Obviously this is just an example and I would really like it to work on an array of customs objects. (Bonus points: why aren't these two examples equivalent?).

7
  • 1
    But this doesn't give the same result: then: what does it give? Wherein does it differ? My guess (been a long time since VB): the division results in doubles instead of integers? Commented Jul 3, 2014 at 13:27
  • The results are quite big, so I didn't include them to avoid clutter... it gives a hundred arrays with one item in each, Commented Jul 3, 2014 at 13:28
  • If you'd have taken a look at the group-key you should've seen (judging from the currently posted answers which support my hunch) that the keys are all doubles / floats instead of integers. Debugging and looking at the actual results comparing them to expected results should've saved you from posting this question ;-) Commented Jul 3, 2014 at 13:30
  • how would you look at the group-key, I'm new to linq, also, how can you debug linq? I haven't worked this out yet... its more of a run it and find out method. which isn't great... what is your suggestion? Commented Jul 3, 2014 at 13:31
  • The .Select(...) after the GroupBy() "strips" the keys from the GroupBy result so probably the easiest would be storing the intermediate results in a variable and first looking at that (like foo = Enumerable....GroupBy(...), then inspect foo with the debugger). Commented Jul 3, 2014 at 13:33

2 Answers 2

5

In C# you have integer division which means that the decimal places are truncated and the result is also an int. That's why it works in C# and not in VB.NET which maintains decimal places since it converts it to Double.

If you want to have the same behaviour in VB.NET you need to use \ instead of /.

See: \ Operator (Visual Basic)

Enumerable.Range(0, 100).Select(Function(Value, Index) New With {Value, Index}) _
    .GroupBy(Function(x) x.Index \ 10) _
    .Select(Function(g) g.Select(Function(p) p.Value).ToList())
Sign up to request clarification or add additional context in comments.

Comments

-1

Other wise try the below

        int currentPosition = 0;
        int maxArrayLimit = 10;

        //Total values 0-103
        List<int> lstNumbers = Enumerable.Range(0, 103).Select(x => x).ToList<int>();

        //to hold splited values 
        List<List<int>> lstSplitedNumbers = new List<List<int>>();

        while (currentPosition < lstNumbers.Count)
        {
            //spliting values based on currentPosition to maxArrayLimit
            List<int> lstCurrentNumbers = lstNumbers.Skip(currentPosition).Take(maxArrayLimit).ToList<int>();
            lstSplitedNumbers.Add(lstCurrentNumbers);
            //rest the current position
            currentPosition += lstCurrentNumbers.Count;
        }

1 Comment

Thanks, but I was looking for a linq example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.