Search code examples
c#linqdbcommand

DbCommand with Linq?


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


Solution

  • 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;