Click here to Skip to main content
12,299,825 members (58,064 online)
Click here to Skip to main content

Stats

89.1K views
1.5K downloads
201 bookmarked
Posted

LINQ FAQ for Newbies

, 17 Jul 2009 CPOL
A LINQ FAQ for newbies.
WindowsFormsLINQQuery
WindowsFormsLINQQuery
bin
Debug
Release
WindowsFormsLINQQuery.exe
WindowsFormsLINQQuery.vshost.exe
WindowsFormsLINQQuery.vshost.exe.manifest
obj
Debug
Refactor
TempPE
Release
Refactor
TempPE
Properties
WindowsFormsLINQQuery.csproj.user
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsLINQQuery
{
    class clsCustomer
    {
        private string strCustomerName="";
        private string strCustomerCode="";
        private string strCity = "";
        public  string CustomerName
        {
            set
            {
                strCustomerName = value;
            }
            get
            {
                return strCustomerName;
            }
        }
        public string customerCode
        {
            set
            { strCustomerCode = value; }
            get
            { return strCustomerCode; }
        }
        public string City
        {
            set
            {
                strCity = value;
            }
            get
            {
                return strCity;
            }
        }
        public clsOrder[] Orders=null;
    }
    class clsOrder
    {
        private string strProductName;
        public string ProductName
        {
            set
            {
                strProductName = value;
            }
            get
            {
                return strProductName;
            }
        }
    }
    class clsLINQ
    {
        clsCustomer[] objCustomer = new clsCustomer[]
         {
            new clsCustomer{CustomerName="Khadak",customerCode="001",City="Mumbai",Orders = new clsOrder[]
                                            {new clsOrder{ProductName="Shirt"},
                                            new clsOrder{ProductName="Socks"}}},
            new clsCustomer{CustomerName="Shiv",customerCode="002",City="Delhi",Orders = new clsOrder[]{new clsOrder{ProductName="Pants"}}},
            new clsCustomer{CustomerName="Raju",customerCode="003",City="Mumbai",Orders = new clsOrder[]{new clsOrder{ProductName="Socks"}}},
            new clsCustomer{CustomerName="Shaam",customerCode="004",City="Delhi",Orders = new clsOrder[]{new clsOrder{ProductName="Shoes"}}}
         };
        
        public IEnumerable<clsCustomer> FireSimpleQuery()
        {
            return from clsCustomer Obj in objCustomer select Obj;
        }
        public IEnumerable<clsCustomer> FireQueryWithWhere(string strCustomerCode)
        {
            return from clsCustomer Obj in objCustomer  where Obj.customerCode == strCustomerCode select Obj;
        }
        public IEnumerable<clsCustomer> FireQueryWithJoin()
        {
            return from clsCustomer ObjCust in objCustomer 
                   from clsOrder ObjOrder in ObjCust.Orders
                   select ObjCust;
        }
        public IEnumerable<clsCustomer> FireQueryWithOrderBy()
        {
            return from clsCustomer ObjCust in objCustomer
                   orderby ObjCust.City
                   select ObjCust;
        }
        public void FireQueryWithGroupBy(ListBox Obj)
        {
            var GroupCustomers = from ObjCust in objCustomer
                        group ObjCust by ObjCust.City into GroupTemp
                   select new {GroupTemp.Key,GroupTemp};
            foreach (var MyTempGroup in GroupCustomers)
            {
                Obj.Items.Add(MyTempGroup.Key);
                foreach (var MyCustomer in MyTempGroup.GroupTemp)
                {
                    Obj.Items.Add(">>>" + MyCustomer.CustomerName + "  " + MyCustomer.customerCode);
                }
            }

        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160525.2 | Last Updated 18 Jul 2009
Article Copyright 2009 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid