Awesome LINQ Impovements In .NET 6.0

In this article, we will learn a few tricks which can be used in our programming skills,

Most new developers are stuck on a few things which are mentioned below,

  1. Creating the segment Like 3-3.
  2. Making the new local group using different fields.
  3. Getting the youngest or oldest.
  4. Getting one specific item.
  5. Getting the last specific item
  6. Getting more than one item, except for 2 items.
  7. Getting all the values after a specific item.
  8. Getting all the specific last values.
  9. Getting all the values between specific items.

When we are creating a segment just like we have a users list and I want to create a segment for 3-3, how can we do it? We have created methods and added the logic, right? So now in .NET 6.0, we don't need to create a new method. Here we can use Chunk and give the number which you want for the segment.

var users = new string[] {
    "Amit",
    "Sumit",
    "Ankit",
    "Aditi",
    "Anjana",
    "Ria"
};
//if we want the segment type of data Like 3-3
var chunks = users.Chunk(3);

Suppose we have 2 different lists or arrays, like names and ages so how you will combine Like (Sumit, 28). So this problem will be solved with Zipping.

 //if we want combine 2 different list
 var users = new string[] {
     "Amit",
     "Sumit",
     "Ankit"
 };
 var userage = new int[] {
     28,
     30,
     32
 };
 var zipdata = names.Zip(userage);
 Console.WriteLine($ "{zipdata}");

Stuck with Youngest and Oldest item from the list, suppose we have user list and want to see the oldest and youngest member of the list, So how to do that? Here you can use MinBy and MaxBy.

//If you want to youngest and oldest member of the list
var members = new [] {
    new FamilyMembers("Amit", 32, 1),
        new FamilyMembers("Sumit", 30, 2),
        new FamilyMembers("Ankit", 28, 3),
};
var youngest = members.MinBy(x => x.age);
var oldest = members.MaxBy(x => x.age);
Console.WriteLine($ "youngest member: {youngest.name}");
Console.WriteLine($ "oldest member: {oldest.name}");
OUTPUT
youngest member: Ankit
oldest member: Amit

Stick with one specific item from the list. I think most developers know how to get that element. Suppose we want to get the 3rd element of the list, so we write the below code

// if we want some specific element Like 3 name of array
var user = new string[] {
    "Amit",
    "Sumit",
    "Ankit",
    "Aditi",
    "Anjana",
    "Ria",
    "Alok",
    "Shivansh",
    "Devansh"
};
var third = user.ElementAt(2); //index inludes 0
Console.WriteLine($ "Third member: {third}");
OUTPUT
Third member: Ankit

If you want to last specific item just like the 3rd last item of the list, we are using (^) and the number which we want.

// if we want last one element Like 3rd Last
var user = new string[] {
    "Amit",
    "Sumit",
    "Ankit",
    "Aditi",
    "Anjana",
    "Ria",
    "Alok",
    "Shivansh",
    "Devansh"
};
var lastthird = user.ElementAt( ^ 3);
Console.WriteLine($ "Last third member: {lastthird}");
OUTPUT
Last third member: Alok

If we skip the first two users and take two users, like you want Ankit, Aditi, you can use like below code...

//if we skip first 2 user and taking 2 user Like Ankit, Aditi
var user = new string[] { "Amit", "Sumit", "Ankit", "Aditi", "Anjana", "Ria", "Alok", "Shivansh", "Devansh" };
var skipandtake = user.Skip(2).Take(2);
Console.WriteLine($"After skipping 2 member take 2 item: {skipandtake}");

If you want all the items after a specific value, suppose we have a user list just like above and I wanted all the values after Ankit, so how do we do it?  You can use 2 dots after the number Like this (3..)

//if we want all the user after 3rd one
var user = new string[] { "Amit", "Sumit", "Ankit", "Aditi", "Anjana", "Ria", "Alok", "Shivansh", "Devansh" };
var afterthirdall = user.Take(3..);
Console.WriteLine($"All items after 3rd member: {afterthirdall}");

If you want all the last items from the specific item, say, I want all the items from the  second to the last value.

//if we want all the items from the 2nd last valus
var user = new string[] { "Amit", "Sumit", "Ankit", "Aditi", "Anjana", "Ria", "Alok", "Shivansh", "Devansh" };
var lastmembers = user.Take(^2..);
Console.WriteLine($"Last member: {lastmembers}"); 

If you want items of the specific list, like you want items between 3rd to 6th, you can use the below code. 

//if we want the user between 3rd to 6th 
var user = new string[] { "Amit", "Sumit", "Ankit", "Aditi", "Anjana", "Ria", "Alok", "Shivansh", "Devansh" };
var bwsecondforth = user.Take(3..6);
Console.WriteLine($"Between 3rd to 6th member: {bwsecondforth}");

That’s it, Happy Coding