Is it possible to do the same using Lambda
for (int i = 0; i < objEntityCode.Count; i++)
{
options.Attributes[i] = new EntityCodeKey();
options.Attributes[i].EntityCode = objEntityCode[i].EntityCodes;
options.Attributes[i].OrganizationCode = Constants.ORGANIZATION_CODE;
}
I mean to say to rewrite the statement using lambda. I tried with
Enumerable.Range(0,objEntityCode.Count-1).Foreach(i=> {
options.Attributes[i] = new EntityCodeKey();
options.Attributes[i].EntityCode = objEntityCode[i].EntityCodes;
options.Attributes[i].OrganizationCode = Constants.ORGANIZATION_CODE; }
);
but not working
I am using C#3.0
Well you can make it simpler with object initializers, to start with:
I would probably leave it at that though… there’s currently no
ForEachextension method onIEnumerable<T>– and for good reasons, although I know it’s not a universally held opinion 😉In this case, you’d still need to know
iin order to setoptions.Attributes[i]– unless you could set the whole ofoptions.Attributesin one go, of course… without knowing about the types involved, it’s pretty hard to advise further.If
options.Attributesis a writable property (e.g. an array), you could use:If
options.Attributesis actually just a property which returns a type with an indexer, that won’t work.