Allan Chua
Allan Chua

Reputation: 10185

Is batch initialization of objects possible with the use of LINQ in C#.net?

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();
}

Upvotes: 0

Views: 379

Answers (5)

xanatos
xanatos

Reputation: 111890

var listOfRooms = Enumerable.Range(0, 10).Select(p => new Rooms()).ToList();

This is a solution for creating a List with 10 Rooms.

Note that in the Select you have access to the index (0...9) (called p). You can use to do fancy logic and initialized Rooms with specific values.

As suggested by Mustafin, there is an "hybrid" Linq expression that can be used:

var listOfRooms = (from p in Enumerable.Range(0, 10) select new Rooms()).ToList();

I don't like this form because it "mixes" the two "styles" of Linq.

Upvotes: 8

Default Writer
Default Writer

Reputation: 2566

Read the Introduction to LINQ queries

As you whould expect, this is a "native" compact Linq syntax:

var listOfRoooms = from i in Enumerable.Range(0, 10) select new Rooms();

As long as there is only tree parts of a query operation:

  1. Obtain the data source.
  2. Create the query.
  3. Execute the query.

Your code is missing significant part - List list initialization ("Obtain the data source"). Default constructor can initialize the list with IEnumerable<T> constructor parameter.

List<Rooms> listOfRooms = new List<Rooms>();
foreach(var room in listOfRooms)
{
  room = new Rooms();
}

I will show you a ways to initialize list and select from this list using LINQ syntax:

Initialization:

List<Rooms> listOfRooms = new List<Rooms>(new Rooms[] 
{
    new Rooms(),
    new Rooms(),
    new Rooms()
});

Select:

foreach(var room in listOfRooms)
{
    if (room != null)
    {
        // do something
    }
}

or

 var list = from r in listOfRooms select r != null;

Upvotes: 0

Enigmativity
Enigmativity

Reputation: 117124

You must have one of two things to answer this question: either a known number of rooms to create or an existing enumerable that you want to create rooms for on a one-to-one basis.

Here are the two options:

List<Rooms> listOfRooms =
    Enumerable
        .Range(0, knownNumber)
        .Select(n => new Rooms())
        .ToList();

and:

List<Rooms> listOfRooms =
    existingEnumerable
        .Select(n => new Rooms())
        .ToList();

Does this answer your question?

Upvotes: 1

Adam Ralph
Adam Ralph

Reputation: 29956

You could take a look at NBuilder.

var listofRooms = Builder<Rooms>.CreateListOfSize(10).Build();

You can also use the fluent API for, amongst other things, assigning certain values to given ranges of the list.

Upvotes: 1

John Weldon
John Weldon

Reputation: 40789

To pre-initialize the list with three rooms a quick and dirty way could be:

List<Rooms> listOfRooms = new List<Rooms>
{
    new Rooms(),
    new Rooms(),
    new Rooms(),
}

Or to create 20:

List<Rooms> listOfRooms = 
    new List<Rooms>(Enumerable.Range(0, 20).Select(ix=>new Rooms()));

Upvotes: 1

Related Questions