请帮我将 LINQ C# 转换为 VB
请有人帮我将此代码从 C# 转换为 VB。这真的让我发疯。
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("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"] = customers;
}
return customers;
}
}
再次感谢您。
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("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"] = customers;
}
return customers;
}
}
Thank you again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我从自动转换工具开始,然后将输出调整为一点点,生成以下 VB.NET 代码:
公平警告: 虽然此代码对我来说编译得很好,但我绝对不是 LINQ 方面的专家。我强烈建议您亲自测试代码,以确保它确实达到您想要的效果!
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!
Cody 的回答很好,但您也许可以利用 VB.NET 的 xml 功能以这种方式编写 LINQ(我也没有测试过):
这只是一种更简单的方式来讨论当前的后代和属性VB 版本。
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):
This is just a simpler way to talk about descendants and attributes in the current version of VB.