Update value of objects with lambda
Approach with caution
You could use the renowned ForEach
extension method in LINQ to iterate through each item and action it accordingly. Essentially it's the same as the Change
method you described, and could look something like this:
ForEach
LINQ Extension Method
public static void ForEach<TSource>(this IEnumerable<TSource> source, Action<TSource> action)
{
foreach(TSource item in source)
{
action(item);
}
}
Implementation
ienumQuestions.Where(e => e.Question.Length > 25).ForEach(e =>
{
e.Question = e.Question.Substring(0, 25) + "...";
});
// or
ienumQuestions.ForEach(e =>
{
if (e.Question.Length > 25)
{
e.Question = e.Question.Substring(0, 25) + "...";
}
});
Argument and Extra Reading
I personally dislike the approach of using the ForEach
LINQ extension to directly update the objects, I prefer to use it for reference purposes only. I would strongly recommend reading "foreach vs ForEach" before adopting it though, as essentially it goes against the principal of LINQ in the sense that it directly modifies objects within the collection, which is what LINQ was not originally intended to do.
Related videos on Youtube
Inx
Updated on June 04, 2022Comments
-
Inx almost 2 years
I have this IEnumerable of type Question.. the class Question has the properties, Question(string) and Id(int)
What I want to do is if the question of any object in the IEnumerable has a length > 25 then "Substring" it and add "..."
So my idea was something like:
ienumQuestions.Where(e=>e.Question.Length > 25).Change(e.Question=>e.Question.Substring(25)+"...");
Now im not sure if that syntax was 100% correct but I hope you get the point..
Is there wanyway to do this?.. i really dont want to create a loop for this.. since I simply dont like loops :)
Thanks in advance!
-
Jodrell almost 12 yearsYou don't like loops? How do you think IEnumerable gets implemented?
-