Login    
 
 
 
 
Text/HTML
  
You are here :: Blogs Saturday, May 19, 2012

Search
Note: This uses the internal blog search engine. The Google search engine is also available at the top of the page.
  
Disclaimer

Please review the site disclaimer before downloading or using content found on this site

  
Categories
  
DEVSHED Blog
As always, I welcome your comments!
Feb 21

Written by: Steve Gray
2/21/2011 3:24 PM  RssIcon

I’m… just not a big fan of LINQ. It’s hard to work with, the syntax is obscure and it’s hard to find documentation for. I’m not sure if it’s something that will be around in the future.

Anyway, I have this task that reoccurs every so often – given a DataTable, I need to group the table by one (or more) of the fields and do something with the group. Often I need to iterate through the lines in the table that match a group.

Note that the first query returns one field, so when you reference that field in ‘salesQueryItem’, you only need strTo = salesQueryItem. In the second loop there is more than one field in the select, so they’re itemized.

 

Code Snippet
  1. Dim oDT As New DataTable
  2.  
  3. 'create columns
  4. oDT.Columns.Add(New DataColumn("salesperson", System.Type.GetType("System.String")))
  5. oDT.Columns.Add(New DataColumn("order", System.Type.GetType("System.String")))
  6. oDT.Columns.Add(New DataColumn("amount", System.Type.GetType("System.Int16")))
  7.  
  8. 'create the rows
  9. oDT.Rows.Add("Bob@test.com", "ORD00100", 1.23)
  10. oDT.Rows.Add("Bob@test.com", "ORD00102", 3.23)
  11. oDT.Rows.Add("Sam@test.com", "ORD00104", 2.23)
  12. oDT.Rows.Add("Sam@test.com", "ORD00105", 4.23)
  13.  
  14.  
  15. Dim strBody As String = ""
  16. Dim strTo As String = ""
  17. Dim strSubject As String = "Product Hold Email Notification"
  18.  
  19. Try
  20.     Dim salesQuery = From sales In oDT _
  21.                Group sales By salesperson1 = sales("salesperson") Into Group _
  22.                Select salesperson1 = salesperson1
  23.  
  24.  
  25.     For Each salesQueryitem In salesQuery
  26.         strTo = salesQueryitem
  27.         strBody = "Orders for " & strTo & "<br />"
  28.  
  29.         Dim Query2 = From emailNotifications In oDT _
  30.                       Where emailNotifications("salesperson") = strTo _
  31.                       Select order1 = emailNotifications("order"), amount1 = emailNotifications("amount").ToString
  32.  
  33.         For Each Query2item In Query2
  34.             strBody += Query2item.order1 + "<br />"
  35.             strBody += "Amount: " & Query2item.amount1 + "<br />"
  36.         Next
  37.  
  38.         'code to send an email here
  39.     Next
  40.  
  41. Catch ex As Exception
  42.     MsgBox(ex.Message)
  43. End Try

Tags:
Categories:
As always, I welcome your comments!
  
 
 
Home | Products | Blogs | Contact Us | Links | God's Plan
Privacy Statement | Terms Of Use
 
Copyright 2011 by Devshed.us