DbCommand with Linq?

1.5k views Asked by At

I'm using DbCommand from : System.ComponentModel.Component

I'm building an object with 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);

And now, I want to iterate it using linq:

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

but it's generating following error :

enter image description here

1

There are 1 answers

0
ken2k On BEST ANSWER
IEnumerable<DbParameter> t = command.Parameters.Cast<DbParameter>();

You have to use Cast<T>() because the type of Parameters is DbParameterCollection, which implements IEnumerable (non-generic) but not IEnumerable<T>. You can write

IEnumerable t = command.Parameters;