-
Jan 20th, 2010, 10:05 AM
#1
Thread Starter
Fanatic Member
Need LINQ querry help
Hi all,
Here is my problem, I have another xml list i need to put onto a dgv, this time the xml structure is like this
Code:
<job>
<id>120045</id>
<title>Take Items to warehouse</title>
<time>
<start>10/10/10 09:23</start>
<duration>24:00</duration>
</time>
<item>
<quantity>2</quantity>
<description>boxes of nw3354/223</description>
<stock>5</stock>
</item>
<item>
<quantity>5</quantity>
<description>Silicon Sealant 150ml</description>
<stock>15</stock>
<item>
more items in the same format
</item>
<issue>
<initials>cas</initials>
<employee_id>276322</employee_id>
</issue>
</job>
<job>
lots more jobs
</job>
so as you can see each job can contain multiple items these i will place on a dgv in a dropdown list or something. My initial try at this ended up with 2 lists one for all the jobs and the 2nd with all the items, which i dont want. How can i do this as a single list? how can i have an unknown amount of items occuring within a single item? i have created a class for all the elements this xml file uses but i have it as 2 seperate classes within a single file (i again presume this is wrong or needs changing somehow)
If debugging is the process of removing bugs, then programming must be the process of putting them in.
-
Jan 20th, 2010, 11:46 AM
#2
Re: Need LINQ querry help
If I was to translate that XML file into classes it would look something like this (in a somewhat meta VB language):
Code:
Class JobItem
Property Quantity As Integer
Property Description As String
Property Stock As Integer
End Class
Class Job
Property ID As Integer
Property Title As String
'... other properties
Property Items As List(Of JobItem) 'or IEnumerable(Of JobItem)
End Class
Now I know this isn't exactly what you asked for, but since you didn't post any code besides the format of the XML file, I'm just winging it here.
So you want a List(Of Job) in where each Job class contains an Items property that is a List(Of JobItems)?
-
Jan 20th, 2010, 11:58 AM
#3
Thread Starter
Fanatic Member
Re: Need LINQ querry help
Originally Posted by Joacim Andersson
If I was to translate that XML file into classes it would look something like this (in a somewhat meta VB language):
Code:
Class JobItem
Property Quantity As Integer
Property Description As String
Property Stock As Integer
End Class
Class Job
Property ID As Integer
Property Title As String
'... other properties
Property Items As List(Of JobItem) 'or IEnumerable(Of JobItem)
End Class
Now I know this isn't exactly what you asked for, but since you didn't post any code besides the format of the XML file, I'm just winging it here.
So you want a List(Of Job) in where each Job class contains an Items property that is a List(Of JobItems)?
that is pretty much what my class structure looks like and yes i guess that is pretty correct a list of job that contains an item that is a list of jobitems. but how do i get this list from the xml with a single querry? can it be done from a single querry?
If debugging is the process of removing bugs, then programming must be the process of putting them in.
-
Jan 20th, 2010, 12:14 PM
#4
Re: Need LINQ querry help
This is how I tested it. First please note that I added a <jobs> root node to your xml file so that it has a single root with several <job> sub nodes (in my example I only have one <job> node).
Please also note that I do not suggest that you use public fields as properties of the class, they should be full blown property procedures, but in this simple example I use that since this is an example and not production code.
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim xml = <jobs>
<job>
<id>120045</id>
<title>Take Items to warehouse</title>
<time>
<start>10/10/10 09:23</start>
<duration>24:00</duration>
</time>
<item>
<quantity>2</quantity>
<description>boxes of nw3354/223</description>
<stock>5</stock>
</item>
<item>
<quantity>5</quantity>
<description>Silicon Sealant 150ml</description>
<stock>15</stock>
</item>
<issue>
<initials>cas</initials>
<employee_id>276322</employee_id>
</issue>
</job>
</jobs>
Dim jobList = From job In xml...<job> _
Select ID = job.<id>.First.Value, _
Title = job.<title>.First.Value, _
Items = From item In job...<item> _
Select Quantity = item.<quantity>.First.Value, _
Description = item.<description>.First.Value
Dim jobs As New List(Of Job)
For Each itm In jobList
Dim j As New Job
'This would be easier if you have a constructor instead of setting each property individually
j.ID = CInt(itm.ID)
j.Title = itm.Title
For Each item In itm.Items
'Here I use a constructor
j.Items.Add(New JobItem(item.Description, CInt(item.Quantity)))
Next
Next
End Sub
End Class
Public Class Job
Public Title As String
Public ID As Integer
Public Items As New List(Of JobItem)
End Class
Public Class JobItem
Public Quantity As Integer
Public Description As String
Sub New(ByVal description As String, ByVal quantity As Integer)
Me.Quantity = quantity
Me.Description = description
End Sub
End Class
Note that this doesn't consume every sub-node in your XML, but again it's just an example.
As you can see the query actually contains another sub-query (so it is actually two queries, or it's rather like a join in SQL).
-
Jan 21st, 2010, 01:31 PM
#5
Thread Starter
Fanatic Member
Re: Need LINQ querry help
cool i'll have a go with this and get back if i have any further issues thanks for the advice joacim
If debugging is the process of removing bugs, then programming must be the process of putting them in.
-
Jan 21st, 2010, 05:20 PM
#6
Thread Starter
Fanatic Member
Re: Need LINQ querry help
Ok I got all the elements from my xml into the List as per the example given above and went to put this onto a DGV. I bound the job object to a datasource and found on looking at the columns that the column i expected to find for Items is not existing. I presume i will be able to use a combobox for this and i am only interested in the quantity and description properties. Any ideas how to do this? i expected (wrongly i guess) that the dgv would be auto populated with all proprties from the joblist.
If debugging is the process of removing bugs, then programming must be the process of putting them in.
-
Jan 22nd, 2010, 06:18 AM
#7
Thread Starter
Fanatic Member
Re: Need LINQ querry help
ok so i'm thinking now that i need to create this column manually and fill the values on the fly in the rowchanged event or something, would i be right in proceeding in this way?
If debugging is the process of removing bugs, then programming must be the process of putting them in.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|