I am wondering if it is possible to merge ToArray
conversion with resulting array single element change into one statement.
This works perfectly
var response = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
response[0].Summary = "Some text";
return response;
However this one, returns only string contained in Summary
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray()[0].Summary = "Some text";
I am wondering if it is possible to merge ToArray
conversion with resulting array single element change into one statement.
This works perfectly
var response = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
response[0].Summary = "Some text";
return response;
However this one, returns only string contained in Summary
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray()[0].Summary = "Some text";
Share
Improve this question
edited Feb 16 at 21:38
Dmitrii Bychenko
187k20 gold badges172 silver badges225 bronze badges
asked Feb 16 at 20:36
Megrez7Megrez7
1,4771 gold badge17 silver badges41 bronze badges
3
- 1 it's possible. but is it necessary? – Ivan Petrov Commented Feb 16 at 20:50
- 4 Your original code is clear. I would leave it as is. – mjwills Commented Feb 16 at 21:12
- 1 Original code is more performant since the comparison is redundant, especially in large dataset – khteh Commented Feb 17 at 6:56
2 Answers
Reset to default 2Well, to have Linq only solution, you can try adding ternary operator:
Summary = index == 1
? "Some text"
: Summaries[Random.Shared.Next(Summaries.Length)]
Code:
return Enumerable
.Range(1, 5)
.Select(index => new WeatherForecast {
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = index == 1
? "Some text" // 1st item is special one
: Summaries[Random.Shared.Next(Summaries.Length)] // business as usual
})
.ToArray();
Another (more general) possibility is an extra Select
in the very end:
return Enumerable
.Range(1, 5)
...
/* A lot of Linq here */
...
.Select((value, index) => index == 0
? "Some text"
: value)
.ToArray();
You can always make extension methods such as this if you have to be on one line:
public static class Extensions {
public static T[] WithChangeAt<T>(this T[] arr, int index, Action<T> change)
where T : class {
ArgumentNullException.ThrowIfNull(arr);
if ((uint)index >= (uint)arr.Length)
throw new ArgumentException("Index is out of bounds for the array");
change(arr[index]);
return arr;
}
}
then
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray()
.WithChangeAt(0, x => x.Summary = "Some text");