SqlParameter[] _parameters = {
new SqlParameter( "@IDNumber", Person.IDNumber ),
new SqlParameter( "@Name", Person.Name ),
new SqlParameter( "@Surname", Person.Surname )
};
How to get the "Name" value from object array without using index? What I want to do is,
oPerson.Name = _parameters.Find(@Name).Value;
Use the following:
_parameters.Single(p => p.ParameterName == "@Name").Value;
This assumes one and only one instance of a SqlParameter with a given ParameterName exists. If you're unsure if one exists, do the following:
var param = _parameters.SingleOrDefault(p => p.ParameterName == "@Name");
if (param != null) { oPerson.Name = param.Value; }
Using Linq, it is easy
string pName = "@Name";
var p = _parameters.FirstOrDefault(x => x.ParameterName == pName);
if(p != null)
oPerson.Name = p.Value.ToString();
I assume that you need this code because you are unsure about the presence or not of your parameter. So using FirstOrDefault allows to test for the result without using directly the Value property if your parameter doesn't exists in the collection
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With