Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Print Sublist with Linq - Sum, Average Sublist with Linq

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Print Sublist with Linq - Sum, Average Sublist with Linq

    ref. https://forum.ninjatrader.com/forum/...-double-method

    PHP Code:
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
                        
    public class Program
    {
        public static void Main()
        {
            List<int> duplList = new List<int> {1, 2, 3, 4, 5, 6, 7};
    
            Console.WriteLine(" ");
            Console.WriteLine("Whole List,tLinq Printed: ");
            Console.WriteLine(" ");
            duplList.ForEach(a =>
            {
                Console.WriteLine(a); // {1, 2, 3} -> 6
            });
            
            Console.WriteLine(" ");
            Console.WriteLine("Sub List, Linq Printed: ");
            Console.WriteLine(" ");
            duplList.Where(s => s > (duplList.Count-6) && s <= (duplList.Count))
                .ToList()
                .ForEach(s => Console.WriteLine(s));
            
            Console.WriteLine(" ");
            Console.WriteLine("Sub List, Linq Summed: ");
            Console.WriteLine(" ");
            Console.WriteLine(duplList.Take( (duplList.Count-6)..(duplList.Count) ).Sum()); // {1, 2, 3} -> 6
    
            Console.WriteLine(" ");
            Console.WriteLine("Sub List, Linq Averaged: ");
            Console.WriteLine(" ");
            Console.WriteLine(duplList.Take( (duplList.Count-6)..(duplList.Count) ).Average()); // {1, 2, 3} -> 6
        }
    }&#8203; 
    
    Output:


    Whole List,tLinq Printed:

    1
    2
    3
    4
    5
    6
    7

    Sub List, Linq Printed:

    2
    3
    4
    5
    6
    7

    Sub List, Linq Summed:

    27

    Sub List, Linq Averaged:

    4.5​
    Tested ok:
    Test your C# code online with .NET Fiddle code editor.


    refs.
    How to select values within a provided index range from a List using LINQ
    I am a LINQ newbie trying to use it to acheive the following: I have a list of ints:- List&lt;int&gt; intList = new List&lt;int&gt;(new int[]{1,2,3,3,2,1}); Now, I want to compare the sum of the ...


    Output a list/other data structure using linq query
    is there a way to do a Console.WriteLine() on a Generic Collection example: List a has: a.Key[0]: apple a.Value[0]: 1 a.Key[1]: bold a.Value[2]: 2 Is there a way to write out the List contents: ...


    Print array with LINQ
    https://stackoverflow.com/a/35800748/10789707

    LINQ Methods


    Every Single LINQ Extension Method With Examples | .NET & C# Essentials
    Last edited by PaulMohn; 09-20-2024, 06:33 AM.

    #2
    Originally posted by PaulMohn View Post
    ref. https://forum.ninjatrader.com/forum/...-double-method

    PHP Code:
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class Program
    {
    public static void Main()
    {
    List<int> duplList = new List<int> {1, 2, 3, 4, 5, 6, 7};
    
    Console.WriteLine(" ");
    Console.WriteLine("Whole List,tLinq Printed: ");
    Console.WriteLine(" ");
    duplList.ForEach(a =>
    {
    Console.WriteLine(a); // {1, 2, 3} -> 6
    });
    
    Console.WriteLine(" ");
    Console.WriteLine("Sub List, Linq Printed: ");
    Console.WriteLine(" ");
    duplList.Where(s => s > (duplList.Count-6) && s <= (duplList.Count))
    .ToList()
    .ForEach(s => Console.WriteLine(s));
    
    Console.WriteLine(" ");
    Console.WriteLine("Sub List, Linq Summed: ");
    Console.WriteLine(" ");
    Console.WriteLine(duplList.Take( (duplList.Count-6)..(duplList.Count) ).Sum()); // {1, 2, 3} -> 6
    
    Console.WriteLine(" ");
    Console.WriteLine("Sub List, Linq Averaged: ");
    Console.WriteLine(" ");
    Console.WriteLine(duplList.Take( (duplList.Count-6)..(duplList.Count) ).Average()); // {1, 2, 3} -> 6
    }
    }&#8203; 
    
    Output:



    Tested ok:
    Test your C# code online with .NET Fiddle code editor.


    refs.
    How to select values within a provided index range from a List using LINQ
    I am a LINQ newbie trying to use it to acheive the following: I have a list of ints:- List&lt;int&gt; intList = new List&lt;int&gt;(new int[]{1,2,3,3,2,1}); Now, I want to compare the sum of the ...


    Output a list/other data structure using linq query
    is there a way to do a Console.WriteLine() on a Generic Collection example: List a has: a.Key[0]: apple a.Value[0]: 1 a.Key[1]: bold a.Value[2]: 2 Is there a way to write out the List contents: ...


    Print array with LINQ
    https://stackoverflow.com/a/35800748/10789707

    LINQ Methods


    Every Single LINQ Extension Method With Examples | .NET & C# Essentials
    https://www.youtube.com/watch?v=7-P6Mxl5elg
    .Net 4.8 GetRange()

    Comment


      #3
      .Net 4.8 GetRange() .ForEach(s => Print(s));

      Code:
      using System;
      using System.Collections.Generic;
      using System.Linq;
                          
      public class Program
      {
          public static void Main()
          {
              List<int> intsList = new List<int> {1, 2, 3, 4, 5, 6, 7};
      
              Console.WriteLine(" ");
              Console.WriteLine("Whole List, Linq Printed: ");
              Console.WriteLine(" ");
              intsList.ForEach(a =>
              {
                  Console.WriteLine(a); // {1, 2, 3} -> 6
              });
              
              Console.WriteLine(" ");
              Console.WriteLine(".Net 4.8 Sub List, Linq Printed: ");
              Console.WriteLine(" ");
              //duplList.Where(s => s > (duplList.Count-6) && s <= (duplList.Count))
                  //.ToList()
                  //.ForEach(s => Console.WriteLine(s));
              intsList.GetRange((intsList.Count-6),6)
                  .ToList()
                  .ForEach(s => Console.WriteLine(s));
              
              Console.WriteLine(" ");
              Console.WriteLine(".Net 4.8 Sub List, Linq Summed: ");
              Console.WriteLine(" ");
              //Console.WriteLine(duplList.Take( (duplList.Count-6)..(duplList.Count) ).Sum()); // {1, 2, 3} -> 6
              Console.WriteLine(intsList.GetRange( (intsList.Count-6),(6) ).Sum());
      
              Console.WriteLine(" ");
              Console.WriteLine(".Net 4.8 Sub List, Linq Averaged: ");
              Console.WriteLine(" ");
              //Console.WriteLine(duplList.Take( (duplList.Count-6)..(duplList.Count) ).Average()); // {1, 2, 3} -> 6
              Console.WriteLine(intsList.GetRange( (intsList.Count-6),(6) ).Average());
          }
      }​
      Output:



      Whole List, Linq Printed:

      1
      2
      3
      4
      5
      6
      7

      .Net 4.8 Sub List, Linq Printed:

      2
      3
      4
      5
      6
      7

      .Net 4.8 Sub List, Linq Summed:

      27

      .Net 4.8 Sub List, Linq Averaged:

      4.5​
      Tested ok:
      https://dotnetfiddle.net/iduIzT

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      576 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      334 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      553 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      551 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X