请帮我将 LINQ C# 转换为 VB

发布于 2024-10-25 17:41:22 字数 1397 浏览 1 评论 0原文

请有人帮我将此代码从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

⒈起吃苦の倖褔 2024-11-01 17:41:22

我从自动转换工具开始,然后将输出调整为一点点,生成以下 VB.NET 代码:

公平警告: 虽然此代码对我来说编译得很好,但我绝对不是 LINQ 方面的专家。我强烈建议您亲自测试代码,以确保它确实达到您想要的效果!

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

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
可是我不能没有你 2024-11-01 17:41:22

Cody 的回答很好,但您也许可以利用 VB.NET 的 xml 功能以这种方式编写 LINQ(我也没有测试过):

    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()  

这只是一种更简单的方式来讨论当前的后代和属性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):

    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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文