Would someone please help me convert this code from C# to VB. This is really driving me crazy.
private List<Customer> Customers
{
get
{
List<Customer> customers = HttpContext.Current.Session["Customers"] as List<Customer>;
// load the customers on first access
if (customers == null)
{
customers = new List<Customer>();
XDocument xDoc = XDocument.Load(HttpContext.Current.Server.MapPath(@"App_Data\customers.xml"));
customers =
(
from c in xDoc.Descendants("customer")
orderby c.Attribute("CustomerID").Value
select new Customer
{
ID = c.Attribute("Cu开发者_开发问答stomerID").Value,
CompanyName = c.Attribute("CompanyName").Value,
ContactName = c.Attribute("ContactName").Value,
ContactTitle = c.Attribute("ContactTitle").Value,
Address = c.Attribute("Address").Value,
City = c.Attribute("City").Value,
State = c.Attribute("State").Value,
ZIPCode = c.Attribute("ZIPCode").Value,
Phone = c.Attribute("Phone").Value
}
).ToList();
// cache the list
HttpContext.Current.Session["Customers"] = customers;
}
return customers;
}
}
Thank you again.
I started with an automated conversion tool, then massaged the output just a little bit, to produce the following VB.NET code:
Fair Warning: Although this code compiles just fine for me, I am anything but an expert in LINQ. I highly recommend that you test the code yourself, just to be sure that it actually does what you want!
Private ReadOnly Property Customers() As List(Of Customer)
Get
' Change the name of this variable, as VB is not case-sensitive
Dim customersList As List(Of Customer) = TryCast(HttpContext.Current.Session("Customers"), List(Of Customer))
' Load the customers on first access
If customersList Is Nothing Then
customersList = New List(Of Customer)()
Dim xDoc As XDocument = XDocument.Load(HttpContext.Current.Server.MapPath("App_Data\customers.xml"))
customersList = (From c In xDoc.Descendants("customer") _
Order By c.Attribute("CustomerID").Value _
Select New Customer() With { _
.ID = c.Attribute("CustomerID").Value, _
.CompanyName = c.Attribute("CompanyName").Value, _
.ContactName = c.Attribute("ContactName").Value, _
.ContactTitle = c.Attribute("ContactTitle").Value, _
.Address = c.Attribute("Address").Value, _
.City = c.Attribute("City").Value, _
.State = c.Attribute("State").Value, _
.ZIPCode = c.Attribute("ZIPCode").Value, _
.Phone = c.Attribute("Phone").Value _
}).ToList()
' Cache the list
HttpContext.Current.Session("Customers") = customersList
End If
Return customersList
End Get
End Property
Cody's answer is fine, but you may be able to take advantage of VB.NET's xml features to write that LINQ this way (I haven't tested this either):
customersList = (From c In xDoc...<customer>
Order By c.@CustomerID
Select New Customer() With {
.ID = c.@CustomerID,
.CompanyName = c.@CompanyName,
.ContactName = c.@ContactName,
.ContactTitle = c.@ContactTitle,
.Address = c.@Address,
.City = c.@City,
.State = c.@State,
.ZIPCode = c.@ZIPCode,
.Phone = c.@Phone
}).ToList()
This is just a simpler way to talk about descendants and attributes in the current version of VB.
精彩评论