Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. How to Linq query using anonymous types to XML?

How to Linq query using anonymous types to XML?

Scheduled Pinned Locked Moved LINQ
questiondatabasecsharplinqxml
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fu Manchu
    wrote on last edited by
    #1

    Hi all, This one always gets me? im using anonymous types because i'm joining several tables together in my linq query, the question is how do i iterate though my collection to store the result to xml. Or is there a way to send the linq result directly? My Query is, and i've simplified it...

    Dim myStock = From stock As StockItem In db.StockItems _
    Join rail As RailStock In db.RailStocks On stock.PK_StockItemID Equals rail.FK_StockItemID _
    Select New With {.ShippingStatus = stock.FK_ShippingStatus, .SerialNumber = rail.StockSerialNumber}

    From here, i'm unsure how to convert the anonymous type to a object that has the scope of my parameters? Obviously , if i do this i just get the last set of elements in the result set

    Dim xml As XElement = Nothing

        For Each item In myStock
    
            xml = <Stock>
    
                      <Shipping><%= item.ShippingStatus %></Shipping>
                      <Serial><%= item.SerialNumber %></Serial>
    
                  </Stock>
    
        Next
    
        xml.Save("C:\\Delete Me\\test.xml")
    

    Thanks anyway Andy

    J 1 Reply Last reply
    0
    • F Fu Manchu

      Hi all, This one always gets me? im using anonymous types because i'm joining several tables together in my linq query, the question is how do i iterate though my collection to store the result to xml. Or is there a way to send the linq result directly? My Query is, and i've simplified it...

      Dim myStock = From stock As StockItem In db.StockItems _
      Join rail As RailStock In db.RailStocks On stock.PK_StockItemID Equals rail.FK_StockItemID _
      Select New With {.ShippingStatus = stock.FK_ShippingStatus, .SerialNumber = rail.StockSerialNumber}

      From here, i'm unsure how to convert the anonymous type to a object that has the scope of my parameters? Obviously , if i do this i just get the last set of elements in the result set

      Dim xml As XElement = Nothing

          For Each item In myStock
      
              xml = <Stock>
      
                        <Shipping><%= item.ShippingStatus %></Shipping>
                        <Serial><%= item.SerialNumber %></Serial>
      
                    </Stock>
      
          Next
      
          xml.Save("C:\\Delete Me\\test.xml")
      

      Thanks anyway Andy

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      Fu Manchu wrote:

      how to convert the anonymous type to a object that has the scope of my parameters?

      I don't understand what this means. Can you clarify?

      Tech, life, family, faith: Give me a visit. The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

      F 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        Fu Manchu wrote:

        how to convert the anonymous type to a object that has the scope of my parameters?

        I don't understand what this means. Can you clarify?

        Tech, life, family, faith: Give me a visit. The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

        F Offline
        F Offline
        Fu Manchu
        wrote on last edited by
        #3

        Sorry this is my fault for trying to make it short and sweet. What i'm trying to say here is the scope of the parameters i.e shippingstatus, serial numbers..etc is lost due to the anonymous typing. Is there an object or collection i can convert myStock to to get the intellisense of the parameters? ie... Dim myStock = From stock As StockItem In db.StockItems _ Join rail As RailStock In db.RailStocks On stock.PK_StockItemID Equals rail.FK_StockItemID _ Select New With {.ShippingStatus = stock.FK_ShippingStatus, .SerialNumber = rail.StockSerialNumber} when you do.. MyStock. -- the intellisence won't find any parmeters, although this is not a problem the end result here is i'm trying to get the data from and sql database into xml - linq is capable of both, so it seems the obvious choice. since the query collection or whatever is called within linq is deferred until you iterate through it, i'm stuck at how to get items out of it? Thanks

        J 1 Reply Last reply
        0
        • F Fu Manchu

          Sorry this is my fault for trying to make it short and sweet. What i'm trying to say here is the scope of the parameters i.e shippingstatus, serial numbers..etc is lost due to the anonymous typing. Is there an object or collection i can convert myStock to to get the intellisense of the parameters? ie... Dim myStock = From stock As StockItem In db.StockItems _ Join rail As RailStock In db.RailStocks On stock.PK_StockItemID Equals rail.FK_StockItemID _ Select New With {.ShippingStatus = stock.FK_ShippingStatus, .SerialNumber = rail.StockSerialNumber} when you do.. MyStock. -- the intellisence won't find any parmeters, although this is not a problem the end result here is i'm trying to get the data from and sql database into xml - linq is capable of both, so it seems the obvious choice. since the query collection or whatever is called within linq is deferred until you iterate through it, i'm stuck at how to get items out of it? Thanks

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          Hmmm, I'm not too familiar with VB or its intellisense capabilities. Couple things: myStock will be a sequence of elements. If you iterate over myStock, you should get some anonymous types with intellisense. In C#, it'd be

          var myStock = ...;
          foreach(var stock in myStock)
          {
          Console.WriteLine(stock.ShippingStatus);
          }

          Are you not seeing this?

          Tech, life, family, faith: Give me a visit. The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

          F 1 Reply Last reply
          0
          • J Judah Gabriel Himango

            Hmmm, I'm not too familiar with VB or its intellisense capabilities. Couple things: myStock will be a sequence of elements. If you iterate over myStock, you should get some anonymous types with intellisense. In C#, it'd be

            var myStock = ...;
            foreach(var stock in myStock)
            {
            Console.WriteLine(stock.ShippingStatus);
            }

            Are you not seeing this?

            Tech, life, family, faith: Give me a visit. The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

            F Offline
            F Offline
            Fu Manchu
            wrote on last edited by
            #5

            Again this is not what i'm asking if you read my first post, i'm trying to do this...

            Dim xml As XElement = Nothing
            For Each item In myStock
            xml = <Stock>
            <Shipping><%= item.ShippingStatus %></Shipping>
            <Serial><%= item.SerialNumber %></Serial>
            </Stock>
            Next
            xml.Save("C:\Delete Me\test.xml")

            But this won't work because, it will iterate through the entire collection and give the last element - because xml will be passed new data on every loop. I stated this at the start though! What i'm asking is can i use my ienumerable variable and pass its contents to a xelement, so i can define the schema of my data as xml....

            dim xml = <Stock>

            <ShippingStatus>some code here </ShippingStatus>
            <SerialNumber>some code here </SerialNumber>

            <Stock>

            Andy

            G 1 Reply Last reply
            0
            • F Fu Manchu

              Again this is not what i'm asking if you read my first post, i'm trying to do this...

              Dim xml As XElement = Nothing
              For Each item In myStock
              xml = <Stock>
              <Shipping><%= item.ShippingStatus %></Shipping>
              <Serial><%= item.SerialNumber %></Serial>
              </Stock>
              Next
              xml.Save("C:\Delete Me\test.xml")

              But this won't work because, it will iterate through the entire collection and give the last element - because xml will be passed new data on every loop. I stated this at the start though! What i'm asking is can i use my ienumerable variable and pass its contents to a xelement, so i can define the schema of my data as xml....

              dim xml = <Stock>

              <ShippingStatus>some code here </ShippingStatus>
              <SerialNumber>some code here </SerialNumber>

              <Stock>

              Andy

              G Offline
              G Offline
              Gideon Engelberth
              wrote on last edited by
              #6

              You are on the right track, you just need to move the iteration inside the xml element. Don't forget that you can nest LINQ queries inside of XML literals.

              Dim xml = <StockList>
              <%= From item In myStock _
              Select <Stock>
              <Shipping><%= item.ShippingStatus %></Shipping>
              <Serial><%= item.SerialNumber %></Serial>
              </Stock> %>
              </StockList>

              F 1 Reply Last reply
              0
              • G Gideon Engelberth

                You are on the right track, you just need to move the iteration inside the xml element. Don't forget that you can nest LINQ queries inside of XML literals.

                Dim xml = <StockList>
                <%= From item In myStock _
                Select <Stock>
                <Shipping><%= item.ShippingStatus %></Shipping>
                <Serial><%= item.SerialNumber %></Serial>
                </Stock> %>
                </StockList>

                F Offline
                F Offline
                Fu Manchu
                wrote on last edited by
                #7

                Thanks, i've just figured this out myself look through a linq book ;) .

                U 1 Reply Last reply
                0
                • F Fu Manchu

                  Thanks, i've just figured this out myself look through a linq book ;) .

                  U Offline
                  U Offline
                  User 2458453
                  wrote on last edited by
                  #8

                  Here is the link. http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups