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
}
}​
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
refs.
How to select values within a provided index range from a List using LINQ
Output a list/other data structure using linq query
Print array with LINQ
https://stackoverflow.com/a/35800748/10789707
LINQ Methods
Every Single LINQ Extension Method With Examples | .NET & C# Essentials

Comment