Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 请帮我把LINQ C转换成VB_.net_Linq_C# To Vb.net - Fatal编程技术网

.net 请帮我把LINQ C转换成VB

.net 请帮我把LINQ C转换成VB,.net,linq,c#-to-vb.net,.net,Linq,C# To Vb.net,有人能帮我把这段代码从C转换成VB吗。这真的快把我逼疯了 private List<Customer> Customers { get { List<Customer> customers = HttpContext.Current.Session["Customers"] as List<Customer>; // load the customers on first access if (cust

有人能帮我把这段代码从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;
   }
}
再次感谢。

我从一个开始,然后稍微调整了一下输出,以生成以下VB.NET代码:

警告:虽然这段代码对我来说编译得很好,但我绝对不是LINQ方面的专家。我强烈建议您自己测试代码,只是为了确保它确实符合您的要求

作为客户列表的私有只读属性客户 收到 '更改此变量的名称,因为VB不区分大小写 Dim customersList As ListOf Customer=TRYCASTTTPCONTEXT.Current.SessionCustomers,ListOf Customer '在首次访问时加载客户 如果CustomerList什么都不是,那么 CustomerList=新的客户列表 Dim xDoc As XDocument=XDocument.LoadHttpContext.Current.Server.MapPathApp_Data\customers.xml CustomerList=来自xDoc.DegenantsCustomer中的c_ 按c.AttributeCustomerID.Value排序_ 用{_ .ID=c.AttributeCustomerID.Value_ .CompanyName=c.AttributeCompanyName.Value_ .ContactName=c.AttributeContactName.Value_ .ContactTitle=c.AttributeContactTitle.Value_ .Address=c.AttributeAddress.Value_ .City=c.AttributeCity.Value_ .State=c.AttributeState.Value_ .ZIPCode=c.AttributeZIPCode.Value_ .Phone=c.AttributePhone.Value_ }托利斯先生 '缓存列表 HttpContext.Current.SessionCustomers=CustomerList 如果结束 返回客户列表 结束 端属性
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中讨论子体和属性的一种更简单的方法。

您还没有问任何问题。你有什么问题?