与Linq的DbCommand?

时间:2012-01-05 10:21:48

标签: c# linq dbcommand

我正在使用DbCommand来自System.ComponentModel.Component

我正在用params构建一个对象:

DbCommand command = _webERPDB.GetStoredProcCommand("mySp");
_webERPDB.AddInParameter(command, "@a", DbType.Int32, policyId);
_webERPDB.AddInParameter(command, "@b", DbType.Int32, appPolicyPrintQaCheckListId);
_webERPDB.AddInParameter(command, "@c", DbType.Int32, createdBy);

现在,我想使用linq迭代它:

IEnumerable<DbParameter> t = from a in command.Parameters select a;

但它会产生以下错误:

enter image description here

1 个答案:

答案 0 :(得分:9)

IEnumerable<DbParameter> t = command.Parameters.Cast<DbParameter>();

您必须使用Cast<T>(),因为Parameters的类型为DbParameterCollection,其实现IEnumerable(非通用)但不实现IEnumerable<T>。你可以写

IEnumerable t = command.Parameters;