Archive

Archive for the ‘Generics’ Category

Reading XML into Generic List of Classes

November 18, 2010 Leave a comment

Public Function GetCustomers() As List(Of Customer)
Dim data As XDocument = XDocument.Load("c:\customers\customers.xml"))

Return (From c In data.Descendants("Customer")
Order By c.Attribute("Surname")
New Customer() With { _
Key .ID = Convert.ToInt32(c.Attribute("ID").Value), _
Key .Forename = c.Element("Forename").Value, _
Key .Surname = c.Element("Surname").Value, _
Key .DOB = c.Element("DOB").Value, _
Key .Location = c.Element("Location").Value _
}).ToList()
End Function

 

Categories: Generics, LINQ Tags: ,

Generic List of Classes to XML using LINQ

November 18, 2010 Leave a comment

Dim _doc As New XDocument(New XDeclaration("1.0", "UTF-8", "yes"), _
 New XElement("EmailAccounts", _
 From EMailAccount In CEmailAccountsList _
 Select New XElement("EMailAccount", _
 New XElement("AccountFromSender", EMailAccount.AccountFromSender), _
 New XElement("EMailAddress", EMailAccount.EMailAddress), _
 New XElement("MailServerAddress", EMailAccount.MailServerAddress), _
 New XElement("MailServerType", EMailAccount.MailServerType), _
 New XElement("PaperFlowImportDirectory", EMailAccount.PFImportDirectory), _
 New XElement("Password", EMailAccount.Password), _
 New XElement("Port", EMailAccount.Port), _
 New XElement("PropertyID", EMailAccount.PropertyID), _
 New XElement("PropertyIDFromSubject", EMailAccount.PropertyIDFromSubject), _
 New XElement("SecurityType", EMailAccount.SecurityType), _
 New XElement("SubjectAsIndex", EMailAccount.SubjectAsIndex), _
 New XElement("Username", EMailAccount.Username))))

 _doc.Save(constEMailSettingsFilename)

Categories: Generics, LINQ Tags: ,

Sorting Dictionary(of String, Integer) Keys

October 11, 2010 Leave a comment

Need to sort a diction by the String Keys?

 


' Get list of keys.
 Dim keys As List(Of String) = EMailSessionTotals.Keys.ToList

 ' Sort the keys.
 keys.Sort()

 ' Loop over the sorted keys.
 Dim str As String
 For Each str In keys
 _counter += 1
 Dim _li As New ListViewItem
 _li.Text = str
 _li.SubItems.Add(EMailSessionTotals.Item(str).ToString)
 If _counter Mod 2 = 0 Then
 _li.BackColor = Color.LightGray
 End If
 lsvSessionTotals.Items.Add(_li)
 Next

Categories: Generics Tags: ,