转换.NET 4.0的语法OfType到.NET 3.5语法、NET、OfType

2023-09-05 23:58:00 作者:花溪信马

我有这样的.NET 4.0 code:

I have this .NET 4.0 code:

var myTable = tables[1];
myTable = tables.Item["Table1"];
myTable = tables.OfType<Excel.ListObject>().FirstOrDefault(t => t.Name == "Table1");

我试图编译.NET 3.5和获得一堆错误:

I am trying to compile with .NET 3.5 and getting a bunch of errors:

错误23属性,索引器或事件'项目'是不支持   语言;尝试直接调用访问方法   Microsoft.Office.Interop.Excel.ListObjects.get_Item(对象)

Error 23 Property, indexer, or event 'Item' is not supported by the language; try directly calling accessor method 'Microsoft.Office.Interop.Excel.ListObjects.get_Item(object)'

错误24Microsoft.Office.Interop.Excel.ListObjects'不包含   对于OfType和最佳推广方法重载的定义   System.Linq.Queryable.OfType(System.Linq.IQueryable)有   一些无效参数

Error 24 'Microsoft.Office.Interop.Excel.ListObjects' does not contain a definition for 'OfType' and the best extension method overload 'System.Linq.Queryable.OfType(System.Linq.IQueryable)' has some invalid arguments

错误25实例参数:无法从转换   Microsoft.Office.Interop.Excel.ListObjects来   System.Linq.IQueryable

TypeScript基本语法

Error 25 Instance argument: cannot convert from 'Microsoft.Office.Interop.Excel.ListObjects' to 'System.Linq.IQueryable'

能否请你指导我正是这些错误的意思,这样我可以尝试将其转换为.NET 3.5?

Can you please guide me on exactly what these errors mean so that I can try to convert this to .NET 3.5?

感谢您这么多的意见。

推荐答案

这个问题指的是给出This命令至少需要两行的源数据的;在code正显示出3个不同的code访问表/的ListObject的方式。

This question refers to an answer given for This command requires at least two rows of source data; the code is showing 3 different ways of accessing a table/listobject in code.

这是code我贴:

    var myTable = tables[1];
    var myTable = tables.Item["Table1"];
    var myTable = tables.OfType<Excel.ListObject>().FirstOrDefault(t => t.Name == "Table1");

我为了说明,有在code访问表的不同方式。

I meant to illustrate that there are different ways of accessing a table in code.

要把它在上下文中,我创建了一个按钮叫TestButton功能区。这里是整个code,应与3.5:

To put it in context, I have created a Ribbon with a single button called "TestButton". Here is the entire code, should work with 3.5:

using Excel = Microsoft.Office.Interop.Excel;


    private void TestButton_Click(object sender, RibbonControlEventArgs e)
    {
        var worksheet = (Excel.Worksheet) Globals.ThisAddIn.Application.ActiveSheet;
        var tables = worksheet.ListObjects;
        var table = tables.Item["Table1"]; // this is the line you are referring to

        //do something with table
    }