"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Thursday, December 19, 2013

Using linq and lambda expression in c#

using linq and lambda expression in c#

using linq and lambda expression in c# to filter data.


  • Select all
  • Select some filed
  • Select filtering data with condition
  • Select Anonymous Type / Casting
  • Merging two columns
  • Ordering
  • Joining two list
  • Skip and Take 




List<MYcls> objCls = new List<MYcls>();
            objCls.Add(new MYcls { Address = "Add1", email = "A@Aa.com", FullName = "A A", ID = 1, Mobile = "9539536943" });
            objCls.Add(new MYcls { Address = "Add2", email = "A@Aasd.com", FullName = "A A", ID = 5, Mobile = "9539536943" });
            objCls.Add(new MYcls { Address = "Add3", email = "A@Addd.com", FullName = "A A", ID = 9, Mobile = "9539536943" });
            objCls.Add(new MYcls { Address = "Add4", email = "A@Aghf.com", FullName = "A A", ID = 7, Mobile = "9539536943" });
            objCls.Add(new MYcls { Address = "Add5", email = "A@Awer.com", FullName = "A A", ID = 8, Mobile = "9539536943" });
            objCls.Add(new MYcls { Address = "Add6", email = "A@Adsfsd.com", FullName = "A A", ID = 11, Mobile = "9539536943" });
            objCls.Add(new MYcls { Address = "Add7", email = "A@Aui.com", FullName = "A A", ID = 18, Mobile = "9539536943" });
            objCls.Add(new MYcls { Address = "Add8", email = "A@Aii.com", FullName = "A A", ID = 21, Mobile = "9539536943" });

            List<MyBus> objBus = new List<MyBus>();
            objBus.Add(new MyBus { BusID = 1, ClsID = 1, Name = "Bus1" });
            objBus.Add(new MyBus { BusID = 2, ClsID = 5, Name = "Bus1" });
            objBus.Add(new MyBus { BusID = 3, ClsID = 21, Name = "Bus2" });
            objBus.Add(new MyBus { BusID = 4, ClsID = 18, Name = "Bus2" });
            objBus.Add(new MyBus { BusID = 5, ClsID = 11, Name = "Bus4" });
            objBus.Add(new MyBus { BusID = 6, ClsID = 7, Name = "Bus4" });
            objBus.Add(new MyBus { BusID = 7, ClsID = 9, Name = "Bus3" });

            // select all

            var _linq = (from o in objCls
                         select o).ToList();

            var _lambda = (objCls.Select(o => o)).ToList();

            _linq = null;
            _lambda = null;

            // select some field

            var _Some_linq = (from o in objCls
                              select (new { o.ID, o.FullName })).ToList();

            var _Some_lambda = objCls.Select(o => new { o.ID, o.FullName }).ToList();

            // Filtering

            _linq = (from o in objCls
                     where o.ID == 1
                     select o).ToList();

            _lambda = (objCls.Where(o => o.ID == 1)).ToList();

            _linq = null;
            _lambda = null;

            //Return Anonymous Type CASTING

            var Ano_linq = (from o in objCls
                            select new
                            {
                                ID_Number = o.ID,
                                Full_Name = o.FullName
                            }).ToList();

            var Ano_lambda = objCls.Select(o => new { ID_Number = o.ID, Full_Name = o.FullName }).ToList();

            _linq = null;
            _lambda = null;

            // Meging two colums
            var Mrg_Linq = (from o in objCls
                            select new { FnameAddress = o.FullName + ',' + o.Address }).ToList();

            var Mrg_lambda = objCls.Select(o => new { FnameAddress = o.FullName + ',' + o.Address }).ToList();

            _linq = null;
            _lambda = null;
            // Ordering

            // 1 .OrderBy ID Ascending
            _linq = (from o in objCls
                     orderby o.ID ascending
                     select o).ToList();

            _lambda = objCls.OrderBy(o => o.ID).ToList();

            _linq = null;
            _lambda = null;
            // 2. OrderBy ID Then Ascending fullname

            _linq = (from o in objCls
                     orderby o.ID, o.FullName
                     descending
                     select o).ToList();

            _lambda = objCls.OrderBy(o => o.ID).ThenByDescending(o => o.FullName).ToList();

            _linq = null;
            _lambda = null;

            // Joining

            var Join_linq = (from o in objCls
                             join b in objBus on
                             o.ID equals b.ClsID
                             orderby o.Address descending
                             select new
                             {
                                 o.ID,
                                 o.FullName,
                                 o.email,
                                 o.Address,
                                 o.Mobile,
                                 b.Name
                             }).ToList();

            var Join_Lambda = objBus.Join(objCls, o => o.ClsID, b => b.ID, (o, b) => new { o.Name, b.FullName, b.email, b.Address, b.Mobile, b.ID }).OrderBy(b => b.Address).ToList();

            Join_linq = null;
            Join_Lambda = null;
            // Skip
            Join_linq = (from o in objCls
                         join b in objBus on
                         o.ID equals b.ClsID
                         orderby o.Address descending
                         select new
                         {
                             o.ID,
                             o.FullName,
                             o.email,
                             o.Address,
                             o.Mobile,
                             b.Name
                         }).Skip(2).ToList();

            Join_Lambda = objBus.Join(objCls, o => o.ClsID, b => b.ID, (o, b) => new { o.Name, b.FullName, b.email, b.Address, b.Mobile, b.ID }).OrderBy(b => b.Address).Skip(2).ToList();

            Join_linq = null;
            Join_Lambda = null;
            // Skip and Take
            Join_linq = (from o in objCls
                         join b in objBus on
                         o.ID equals b.ClsID
                         orderby o.Address descending
                         select new
                         {
                             o.ID,
                             o.FullName,
                             o.email,
                             o.Address,
                             o.Mobile,
                             b.Name
                         }).Skip(2).Take(2).ToList();

            Join_Lambda = objBus.Join(objCls, o => o.ClsID, b => b.ID, (o, b) => new { o.Name, b.FullName, b.email, b.Address, b.Mobile, b.ID }).OrderByDescending(b => b.Address).Skip(2).Take(2).ToList();


    

    public class MYcls
    {
        public int ID { get; set; }
        public string FullName { get; set; }
        public string Address { get; set; }
        public string Mobile { get; set; }
        public string email { get; set; }
    }

    public class MyBus
    {
        public int BusID { get; set; }
        public int ClsID { get; set; }
        public string Name { get; set; }
    }  


If u had any trouble just ask, Happy to help u :)
Stay Tune...
Have a nice day... 'N happy Coding :)

No comments: