简体   繁体   English

从VB转换的C#不起作用

[英]C# converted from VB doesn't work

I used code converter to go from VB to C# and I get errors in c#. 我使用代码转换器从VB转到C#,但在C#中遇到错误。 specifically, error on Item and on string.join(",", Flop.ToArray) . 具体来说, Itemstring.join(",", Flop.ToArray) Error says it doesn't contain a definition for item but it works in VB. 错误说,它不包含一个定义item ,但它在VB中的作品。

VB VB

Dim Flop As New List(Of String)
    For x As Integer = 0 To Dataset9.Tables(0).Rows.Count - 1 'ROWS
        Flop.Add(Dataset9.Tables(0).Rows(x).Item("Id"))
    Next
    strAllRoleNames = String.Join(",", Flop.ToArray)

C# C#

List<string> Flop = new List<string>();

    for (int x = 0; x <= Dataset9.Tables[0].Rows.Count - 1; x++)
      {
         Flop.Add(Dataset9.Tables[0].Rows[x].Item["Id"]);
      }
 strAllRoleNames = string.Join(",", Flop.ToArray);

Try this: 尝试这个:

List<string> Flop = new List<string>();

    for (int x = 0; x <= Dataset9.Tables[0].Rows.Count - 1; x++)
      {
         Flop.Add(Dataset9.Tables[0].Rows[x]["Id"].ToString());
      }
 strAllRoleNames = string.Join(",", Flop.ToArray());

They three keys that were missing here 他们在这里缺少的三个钥匙

  1. Accessing the item in a row, you need to use the C# default indexer as Item doesn't exist in C# 连续访问该项目,您需要使用C#默认索引器,因为C#中不存在该项目
  2. Since the cell in a row is an object and you want a string, need to explicitly call ToString 由于行中的单元格是一个对象,并且您需要一个字符串,因此需要显式调用ToString
  3. When calling ToArray, you need the () at the end in C# 调用ToArray时,您需要在C#结尾

尝试...

Flop.Add(Dataset9.Tables[0].Rows[x]["Id"].ToString());

ToArray is a method() ToArray是一个method()

List<string> Flop = new List<string>();

    for (int x = 0; x <= Dataset9.Tables[0].Rows.Count - 1; x++)
      {
        Flop.Add(Dataset9.Tables[0].Rows[x]["Id"]);
      }
 strAllRoleNames = string.Join(",", Flop.ToArray());

In a more concise way you can try below instead: 您可以尝试以更简洁的方式尝试以下操作:

strAllRoleNames = string.Join(",", Dataset9.Tables[0].AsEnumerable()
                                   .Select(C => Convert.ToString(C["Id"]))
                                   .ToArray());

Try the below changes: 尝试以下更改:

Dataset9.Tables[0].Rows[x].Item["Id"] => Dataset9.Tables[0].Rows[x]["Id"]

Flop.ToArray => Flop.ToArray()    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM