How to Linq query using anonymous types to XML?
-
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
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> -
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> -