存储过程列表运行时转换

时间:2014-01-23 20:15:16

标签: c# .net wcf .net-4.5

在我的'MyOtherViewModel'内,我有以下代码

投掷错误:Argument Type myStoredProcedure not Assignable to parameter type MyDbTable

抛出@ (this, c)编译时错误

var query = myWCFClient.myStoredProcedure();
//Some Other Stuff                                          
query.ToList().ForEach(c => myCollection.Add(new MyViewModel(this, c)));

内部MyViewModel我有:

public MyViewModel(MyOtherViewModel owner, MyDbTable table)
       :base(owner, "String")
{
        //other stuff
}

然后我尝试

var t = ((IEnumerable)query).Cast<MyDbTable>().ToList();
t.ToList().ForEach(c => mCollection.Add(new MyViewModel(this, c)));

获取:Unable to cast object of type 'myStoredProcedure' to type 'MyDbTable'.运行时错误

使用WCF在VS 2012c# .Net 4.5中处理此问题的最佳方法是什么?

程序为myStoredProcedure[],这将返回0

的计数
var t = query.OfType<MyDbTable>().ToList();

1 个答案:

答案 0 :(得分:3)

在第一段代码中,您将myStoredProcedure类型的对象放在MyViewModel的构造函数中,而该构造函数需要类型为MyDbTable的对象。这就是为什么你在这段代码上得到编译时错误。

您尝试将myStoredProcedure投射到MyDbTable的第二段代码。这就是为什么你在这段代码上得到运行时异常的原因。

您的WCF服务正在返回超出预期的其他对象。调查服务或更改MyViewModel的构造函数以接受类myStoredProcedure的对象。