为什么我查询不到Microsoft.Office.Interop.Excel.Connections?

Why can't I query Microsoft.Office.Interop.Excel.Connections?

Connections 对象是一个 IEnumerable,每个:

https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.connections.aspx

我只想删除 foreach 中带有 lambda 表达式的注释行

这个:

    foreach (WorkbookConnection connection in book.Connections)
    {
        if (connection.Type != XlConnectionType.xlConnectionTypeOLEDB) continue;
        var conString = connection.OLEDBConnection.Connection.ToString();
        if (conString.Contains("Initial Catalog") && conString.Contains("Data Source"))
            connection.OLEDBConnection.Connection = conString.Replace(ExlCubeServer,
                Settings.Default.OLAPServer[1]);
    }

类似于:

    foreach (WorkbookConnection connection in book.Connections.Cast<Connections>().Where(c => c.Type == XlConnectionType.xlConnectionTypeOLEDB))
    {
        //if (connection.Type != XlConnectionType.xlConnectionTypeOLEDB) continue;
        var conString = connection.OLEDBConnection.Connection.ToString();
        if (conString.Contains("Initial Catalog") && conString.Contains("Data Source"))
            connection.OLEDBConnection.Connection = conString.Replace(ExlCubeServer,
                Settings.Default.OLAPServer[1]);
    }
book.Connections.ToList().Where(c => c.Type == XlConnectionType.xlConnectionTypeOLEDB).ToList().ForEach(connection => PerformAction(connection));

public static void PerformAction(WorkbookConnection connection)
{
    var conString = connection.OLEDBConnection.Connection.ToString();

    if (conString.Contains("Initial Catalog") && conString.Contains("Data Source"))
        connection.OLEDBConnection.Connection = conString.Replace(ExlCubeServer,
            Settings.Default.OLAPServer[1]);
}

Why can't I query Microsoft.Office.Interop.Excel.Connections?

您误解了 Enumerable.Cast<TResult> 泛型类型参数。

根据文档:

Syntax:

public static IEnumerable<TResult> Cast<TResult>(this IEnumerable source)

Return Value:

An IEnumerable<T> that contains each element of the source sequence cast to the specified type.

简而言之,TResult 是集合 element 的类型,而不是您尝试使用的集合类型。

因此将 .Cast<Connections>() 更改为 .Cast<WorkbookConnection>() 将解决问题:

foreach (var connection in book.Connections.Cast<WorkbookConnection>()
    .Where(c => c.Type == XlConnectionType.xlConnectionTypeOLEDB))
{
    var conString = connection.OLEDBConnection.Connection.ToString();
    if (conString.Contains("Initial Catalog") && conString.Contains("Data Source"))
        connection.OLEDBConnection.Connection = conString.Replace(ExlCubeServer,
            Settings.Default.OLAPServer[1]);
}

P.S。我想知道是什么阻止了您首先解决问题。

当我将你的示例代码引入 VS 时

Workbook book = null;
foreach (WorkbookConnection connection in book.Connections.Cast<Connections>().Where(c => c.Type == XlConnectionType.xlConnectionTypeOLEDB))
{
}

我收到指向 c.Type

的编译器错误

Error CS1061: 'Connections' does not contain a definition for 'Type' and no extension method 'Type' accepting a first argument of type 'Connections' could be found (are you missing a using directive or an assembly reference?)

这清楚地表明 c 不是您所期望的,并且确实用鼠标悬停它表明 cConnections.

类型