Currently coding in C#, I wonder if there is a way to factor the code as presented below

```
Entity1 = GetByName("EntityName1");
Entity2 = GetByName("EntityName2");
Entity3 = GetByName("EntityName3");

```

The idea would be to get a single call in the code, factoring the code by placing the `entities` and the `strings` in a container and iterating on this container to get a single "`GetByName()`" line. Is there a way to do this?
['https://Stackoverflow.com/questions/29765504', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4311207/']
You can use LINQ:

```
var names=new[]{"EntityName1","EntityName2","EntityName3",.....};
var entities=names.Select(name=>GetByName(name)).ToArray();

```

Without `ToArray`, `Select` will return an IEnumerable that will be reevalueated each time you enumerate it - that is, `GetByName` will be called each time you enumerate the enumerable.

`ToArray()` or `ToList` will create an array (or list) you can use multiple times. 

You can also call `ToDictionary` if you want to be able to access the entities by name:

```
var entities=names.ToDictionary(name=>name,name=>GetByName(name));

```

All this assumes that the entities don't already exist or that `GetByName` has to do some significant work to retrieve them. If the entities exist you can simply put them in a `Dictionary<String,Entity>`. If the entities have a `Name` property you can use `ToDictionary` to create the dictionary in one statement:

```
var entities=new []{entity1,entity2,entity3,...};
var entitiesDict=entities.ToDictionary(entity=>entity.Name,entity=>entity);

```
Ok, here is an idea.

You can declare this function.

```
IReadOnlyDictionary<string, T> InstantiateByName<T>(
        Func<string, T> instantiator
        params string[] names)
{
    return names.Distinct().ToDictionary(
        name => name,
        name => instantiator(name))
}

```

which you could call like this,

```
var entities = InstantiateByName(
         GetByName,
         "EntityName1",
         "EntityName2",
         "EntityName3");

```

---

To push the over-engineering to the next level,

you can install the Immutable Collections package,

```
PM> Install-Package Microsoft.Bcl.Immutable

```

and modify the function slightly,

```
using Microsoft.Immutable.Collections;

IReadOnlyDictionary<string, T> InstantiateByName<T>(
        Func<string, T> instantiator
        params string[] names,
        IEqualityComparer<string> keyComparer = null,
        IEqualityComparer<T> valueComparer = null)
{
    if (keyComparer == null)
    {
        keyComparer = EqualityComparer<string>.Default;
    }

    if (valueComparer == null)
    {
        valueComparer = EqualityComparer<T>.Default;
    }

    return names.ToImmutableDictionary(
        name => name,
        name => instantiator(name),
        keyComparer,
        valueComparer);
}

```

The function would be used in the exactly the same way. However, the caller is responsible for passing unique keys to the function but, an alternative equality comparer can be passed.