Saturday, 20 November 2021

Generic List C#

 A generic list is a dynamic array-like structure used to store user information for any type of data . The list is one of the most powerful and useful data structures provided by C#. You can not imagine modern programming without a generic list because built-in programming capability makes a developer work easy. You will get some practical real-life programing practice from the list in this post.

public class Student

    {

        public string roll { get; set; }

        public string name { get; set; }

        public string city { get; set; }

    }


    class Program

    {

        static void Main(string[] args)

        {

            List<Student> data = new List<Student>

            {

                new Student{roll="101",name="Ram",city="Vns"},

                new Student {roll="102",name="Sita",city="Alld"},

                new Student {roll="103",name="Anita",city="Vns"}

            };

           foreach(var item in data)

           {

               Console.WriteLine("Roll={0}, Name={1}, City={2}", item.roll, item.name, item.city);

           }

Get Single Item from Search argument (Get Name by Roll No.)

data[data.FindIndex(x => x.roll == "101")].name;


///Removed Item


           data.RemoveAt(data.FindIndex(e => e.roll == "101"));

           Console.WriteLine("\nAfter romoved an item \n");

           foreach (var item in data)

           {

               Console.WriteLine("Roll={0}, Name={1}, City={2}", item.roll, item.name, item.city);

           }

 

           

        }

  Sum


ViewBag.SumDR = data.Sum(p => Convert.ToInt32(p.DebitAmount));

Not in query


data.Where(x=>x.roll !="101" && x.roll != "103" )

This query will skip roll no 101 and 103 


Where Clause

data.Where(x => x.roll== 101).ToList();


    Home

No comments:

Post a Comment