简体   繁体   English

将每个列表项复制到另一个列表项属性

[英]Copy each list item to another list item attribut

Is there an easy way, and most importantly, more optimized to do the fowling code: 有没有一种简单的方法,最重要的是,它经过了更优化以执行错误处理代码:

class Chair{
    int numberOfLegs=4;
}

class House{
    Chair chair;
    String name="My Home";
}

// add chairs to each house
IList<Chair> chairs = new List<Chair>(10); // let us imagine that we have 10 different chairs...

// Code to replace:
IList<House> houses = new List<House>(chairs.Count());
for (int i = 0; i < houses.Count(); i++){
    houses[i].chair = chairs[i]
}

My own suggestion it is replacing the for with Linq 我自己的建议被替换forLinq

// Code to replace:
IList<Homes> homes = chairs.Select(c => new Home{ Chair = c}).ToList();

Is it faster than the old code? 它比旧代码快吗?

Do you have a better idea? 你有更好的主意吗? Maybe using chairs.AsParallel ? 也许使用chairs.AsParallel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM