Monday, 9 July 2018

014 LINQ Nth Record


Finding N th record is very common query.It is asked during interview as well as useful during practical application.LINQ facilitate to find N th record of a collection.Below is the example.

-----------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data;
using System.Linq;
using System.Collections;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication1
{
           class Program
           {
              static void Main(string[] args)
             {
                 var students = new student[]
                      {
                                new student{name = "John1",roll=123,age=12},
                                new student{name = "John2",roll=124,age=13},
                                new student{name = "John3",roll=125,age=14},
                                new student{name = "John4",roll=126,age=15},
                                new student{name = "John5",roll=127,age=16},
                                new student{name = "John6",roll=128,age=17},
                                new student{name = "John7",roll=129,age=18}
                     };


Console.WriteLine("*************LINQ nth record(Lamba Expression)***********/");


                int n = 3;
                var query = students.ElementAtOrDefault(n - 1);

                Console.WriteLine("Name :{0} ,Age :{1},Roll :{2}", query.name, query.age, query.roll);

                Console.ReadKey();

            }

       }

}

public class student
{
          public string name { get; set; }
          public int roll { get; set; }
          public int age { get; set; }
}



*************LINQ nth record(Lamba Expression)***********/
Name :John3 ,Age :14,Roll :125
-------------------------------------------------


No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...