locked
Split ObservableCollection<object> into 2 Equal Parts RRS feed

  • Question

  • Split ObservableCollection<object> into Equal Parts..

    Example : If I Give 2


    Example 2 : If I pass Variable 4

    It should make 4 Parts of List...

    • Edited by ID GO Monday, March 12, 2018 10:02 AM
    Monday, March 12, 2018 10:01 AM

Answers

  • You cannot split a list of items unless you know the total # of items. That is the important part. If you cannot get the total count (because it is dynamic) then you cannot split it. G

    Given the total size use Take and Skip to get the subset.

    //Not tested
    public static IEnumerable<IEnumerable<T>> Partition<T> ( IEnumerable<T> source, int partitionCount )
    {
        if (partitionCount == 1)
            yield return source;
    
        var count = source.Count();
    
        var partitionSize = (count / partitionCount) + 1;
    
        for (var index = 0; index < partitionCount; ++index)
            yield return source.Skip(partitionSize * index).Take(partitionSize);
    }


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by ID GO Monday, March 12, 2018 5:15 PM
    Monday, March 12, 2018 4:43 PM

All replies

  • I tried Below Code...None of them Worked as my expectation.

     public static List<List<T>> SplitList<T>(List<T> me, int size = 2)
            {
                var list = new List<List<T>>();
                for (int i = 0; i < me.Count; i += size)
                    list.Add(me.GetRange(i, Math.Min(size, me.Count - i)));
                return list;
            }

     var mm = DtDv.SelectedItems.ToList();
                var kaak = SplitList(mm, 2);

     public static List<List<T>> ChunkBy<T>(List<T> source, int chunkSize)
            {
                return source
                    .Select((x, i) => new { Index = i, Value = x })
                    .GroupBy(x => x.Index / chunkSize)
                    .Select(x => x.Select(v => v.Value).ToList())
                    .ToList();
            }

     var mm = DtDv.SelectedItems.ToList();
                var kaak = ChunkBy(mm, 4);

    All of the Above Code not Delivering my Expectation.


    • Edited by ID GO Monday, March 12, 2018 10:33 AM
    Monday, March 12, 2018 10:05 AM
  • You cannot split a list of items unless you know the total # of items. That is the important part. If you cannot get the total count (because it is dynamic) then you cannot split it. G

    Given the total size use Take and Skip to get the subset.

    //Not tested
    public static IEnumerable<IEnumerable<T>> Partition<T> ( IEnumerable<T> source, int partitionCount )
    {
        if (partitionCount == 1)
            yield return source;
    
        var count = source.Count();
    
        var partitionSize = (count / partitionCount) + 1;
    
        for (var index = 0; index < partitionCount; ++index)
            yield return source.Skip(partitionSize * index).Take(partitionSize);
    }


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by ID GO Monday, March 12, 2018 5:15 PM
    Monday, March 12, 2018 4:43 PM