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 EntityCo开发者_如何转开发deKey();
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:
for (int i = 0; i < objEntityCode.Count; i++)
{
options.Attributes[i] = new EntityCodeKey
{
EntityCode = objEntityCode[i].EntityCodes,
OrganizationCode = Constants.ORGANIZATION_CODE
};
}
I would probably leave it at that though... there's currently no ForEach
extension method on IEnumerable<T>
- and for good reasons, although I know it's not a universally held opinion ;)
In this case, you'd still need to know i
in order to set options.Attributes[i]
- unless you could set the whole of options.Attributes
in one go, of course... without knowing about the types involved, it's pretty hard to advise further.
If options.Attributes
is a writable property (e.g. an array), you could use:
options.Attributes = objEntityCode.Select(code => new EntityCodeKey
{
EntityCode = code.EntityCodes,
OrganizationCode = Constants.ORGANIZATION_CODE
}).ToArray();
If options.Attributes
is actually just a property which returns a type with an indexer, that won't work.
Enumerable.Range(0, objEntityCode.Count - 1).ToList().ForEach(i =>
{
options.Attributes[i] = new EntityCodeKey();
options.Attributes[i].EntityCode = objEntityCode[i].EntityCodes;
}
);
Enumerable.Range(0, objEntityCode.Count - 1).ToList().ForEach(i =>
{
options.Attributes[i] = new EntityCodeKey
{
EntityCode = objEntityCode[i].EntityCodes
, OrganizationCode = Constants.ORGANIZATION_CODE
};
}
);
精彩评论