I have this object of mine named Rooms can i batch initialized them using linq instead of using this kind of code?
List<Rooms> listOfRooms = new List<Rooms>();
foreach(var room in listOfRooms)
{
room = new Rooms();
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is a solution for creating a
Listwith 10Rooms.Note that in the
Selectyou have access to the index (0…9) (calledp). You can use to do fancy logic and initializedRoomswith specific values.As suggested by Mustafin, there is an “hybrid” Linq expression that can be used:
I don’t like this form because it “mixes” the two “styles” of Linq.