在筛选的待处理更改列表上转换转换错误

时间:2013-12-18 16:01:21

标签: c# tfs .net

问题背景:

我正在以编程方式检入VS 2012 TFS中的文件,并开发了以下代码以根据特定文件名过滤待处理的更改:

internal int CheckinTfsQaItem(IVersionControlItem tfsItem)
    {
        //Get the current workspace info.
        var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(_checkedoutTfsItem.VcQaFolder);

        //Get the TFS project object from the specified server path.
        var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri);

        //Get the workspace.
        var workspace = workspaceInfo.GetWorkspace(server);

        //Filter the pendingChanges object to checkin in only the specified checked out file by it's name.
        PendingChange[] pendingChanges = (PendingChange[])workspace.GetPendingChanges().Where(x => x.FileName == tfsItem.Name);

        //Check in the change, dont set a comment.
        return workspace.CheckIn(pendingChanges, null);
    }

问题:

Intellisense没有显示任何错误,但是当我调试时,我在尝试将PendingChange单个对象转换为PendingChange对象的单个列表(即PendingChanges数组)时收到强制转换异常错误。

以下是抛出的错误:

结果讯息:
测试方法ADPTestProject.TFStests.Check_Facade_CheckIn_Method抛出异常: System.InvalidCastException:无法转换'WhereArrayIterator`1 [Microsoft.TeamFoundation.VersionControl.Client.PendingChange]'totype'类型的对象'Microsoft.TeamFoundation.VersionControl.Client.PendingChange []'。

有谁能告诉我为什么这个演员会失败?

3 个答案:

答案 0 :(得分:1)

PendingChange[] pendingChanges = (PendingChange[])workspace.GetPendingChanges().Where(x => x.FileName == tfsItem.Name);

只需在此行的末尾添加.ToArray()

并删除演员。

所以

var pendingChanges = workspace.GetPendingChanges()
                              .Where(x => x.FileName == tfsItem.Name)
                              .ToArray();

答案 1 :(得分:1)

使用.ToArray()这会将您的查询返回到PendingChange

的数组中
PendingChange[] pendingChanges = workspace.GetPendingChanges().Where(x => x.FileName == tfsItem.Name).ToArray();

答案 2 :(得分:1)

应该是

workspace.GetPendingChanges().Where(x => x.FileName == tfsItem.Name).ToArray(); //<--