【问题标题】:How to perform the below mentioned task(vb.net)?如何执行以下提到的任务(vb.net)?
【发布时间】:2015-03-18 13:32:25
【问题描述】:
Dim cells = From Qmaxminslist As DataGridViewRow In DataGridView2.Rows.Cast(Of DataGridViewRow)() Take Integer.Parse(daysamaxminsmv.Text)

For Each row As DataGridViewRow In cells
   // piece of code...
Next

我已将此代码在 C# 中转换为:

dynamic cells = from Qmaxminslist in DataGridView2.Rows.Cast<DataGridViewRow>() select (int.Parse(daysamaxminsmv.Text));

foreach (DataGridViewRow row in cells)
{
    // piece of code...
}

但在 foreach 语句中有一个例外:

“无法将类型 'int' 转换为 'System.Windows.Forms.DataGridViewRow'”

如何解决? 任何帮助将不胜感激。谢谢

【问题讨论】:

  • 在c#中有select,在vb中有take
  • 重读你的标题,问问自己这对未来的读者是否有帮助。

标签: c# vb.net linq-to-objects


【解决方案1】:

您没有正确转换它。 VB代码选择DataGridViewRows,C#代码选择ints。你想取 N 行。 VB.NET 直接在 LINQ 查询中支持@​​987654323@,C# 不支持。所以我会在 C# 中使用方法语法。

所以这可以按需要工作(不要将 Dim 翻译成 dynamic 而是 var):

int takeRows = int.Parse(daysamaxminsmv.Text);
var rows = DataGridView2.Rows.Cast<DataGridViewRow>().Take(takeRows);

foreach (DataGridViewRow row in rows)
{
    // piece of code...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多