Search This Blog

Tuesday, July 2, 2013

How can use Group by in linq with XML file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
//***Add below class for XElement
using System.Xml;
using System.Text;
using System.Xml.Linq;
/*LINQ—language integrated query—introduces many extensions methods to the standard C# environment. 
  These methods work on Lists, arrays and collections that are not yet in memory. 
*/

public partial class Linq : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
        test();
    }

   private void test()
    {
        XDocument doc = XDocument.Parse(@"<root>
  <RoomRates>
      <RoomPurchaseToken>73516</RoomPurchaseToken>
      <RoomCode>sgl</RoomCode>
      <RoomType>Single Standard Room Bed and Breakfast</RoomType>
      <Adults>45</Adults>
      <Children>0</Children>
      <Quantity>1</Quantity>
      <Price>1255</Price>
      <CurrencyCode>USD</CurrencyCode>
      <Type>Available</Type>
      <OfferType />
      <HotelCode>35</HotelCode>
    </RoomRates>
 <RoomRates>
      <RoomPurchaseToken>73517</RoomPurchaseToken>
      <RoomCode>sgl</RoomCode>
      <RoomType>Single Deluxe Room  Bed and Breakfast</RoomType>
      <Adults>1</Adults>
      <Children>0</Children>
      <Quantity>1</Quantity>
      <Price>127</Price>
      <CurrencyCode>USD</CurrencyCode>
      <Type>Available</Type>
      <OfferType />
      <HotelCode>35</HotelCode>
    </RoomRates>
 <RoomRates>
      <RoomPurchaseToken>734532203</RoomPurchaseToken>
      <RoomCode>sgl</RoomCode>
      <RoomType>Single Deluxe Rooms  Bed and Breakfast</RoomType>
      <Adults>1</Adults>
      <Children>0</Children>
      <Quantity>1</Quantity>
      <Price>158</Price>
      <CurrencyCode>USD</CurrencyCode>
      <Type>Available</Type>
      <OfferType />
      <HotelCode>88932</HotelCode>
    </RoomRates>
    <RoomRates>
      <RoomPurchaseToken>734532204</RoomPurchaseToken>
      <RoomCode>sgl</RoomCode>
      <RoomType>Single Premium Rooms  Bed and Breakfast</RoomType>
      <Adults>1</Adults>
      <Children>0</Children>
      <Quantity>1</Quantity>
      <Price>176</Price>
      <CurrencyCode>USD</CurrencyCode>
      <Type>Available</Type>
      <OfferType />
      <HotelCode>88932</HotelCode>
    </RoomRates>
    <RoomRates>
      <RoomPurchaseToken>734532205</RoomPurchaseToken>
      <RoomCode>sgl</RoomCode>
      <RoomType>Single Junior Suites  Bed and Breakfast</RoomType>
      <Adults>1</Adults>
      <Children>0</Children>
      <Quantity>1</Quantity>
      <Price>223</Price>
      <CurrencyCode>USD</CurrencyCode>
      <Type>Available</Type>
      <OfferType />
      <HotelCode>88932</HotelCode>
    </RoomRates>
    <RoomRates>
      <RoomPurchaseToken>734532207</RoomPurchaseToken>
      <RoomCode>sgl</RoomCode>
      <RoomType>Single Executive Suites Bed and Breakfast</RoomType>
      <Adults>1</Adults>
      <Children>0</Children>
      <Quantity>1</Quantity>
      <Price>295</Price>
      <CurrencyCode>USD</CurrencyCode>
      <Type>On Request</Type>
      <OfferType />
      <HotelCode>88932</HotelCode>
    </RoomRates>
</root>");

        var query =
                from RoomRates in doc.Root.Elements("RoomRates")
                group RoomRates by (int)RoomRates.Element("HotelCode") into g1
                select new
                {
                    HotelCode = g1.Key,
                    AllValues = from alldata in g1
                                //from alldata in g1.Elements("Price")
                                //select (string)alldata
                                select new 
                                {
                                    price = (string)alldata.Element("Price"),
                                    Children = (string)alldata.Element("Children"),
                                    CurrencyCode = (string)alldata.Element("CurrencyCode"),
                                }
                };
        

            foreach (var item in query)
            {
                Response.Write("Key:" + item.HotelCode+ "</br>");
                foreach (var alldata in item.AllValues)
                {
                    Response.Write(alldata + "</br>");
                }
                Console.WriteLine();
            }
        }
    }
OUTPUT:

Key:35
{ price = 1255, Children = 0, CurrencyCode = USD }
{ price = 127, Children = 0, CurrencyCode = USD }
Key:88932
{ price = 158, Children = 0, CurrencyCode = USD }
{ price = 176, Children = 0, CurrencyCode = USD }
{ price = 223, Children = 0, CurrencyCode = USD }
{ price = 295, Children = 0, CurrencyCode = USD }

No comments :