1

Can the following be done in LINQ as a one-liner, without using a for-loop?

IEnumerable<MyObj> MyList = EF.GetList(etc);
foreach (var row in MyList)
{
    row.uploadedAt = MyUtils.ConvertToLocalTime(row.uploadedAtUTC, clientTimezone);
}
//return MyList, which now has both dates in LocalTime and UTC
4
  • 1
    Attached duplicate has a couple of solution, but iterating using foreach seems to the cleanest way. Btw, there is no magic in Linq, it's still using looping under the hood Commented Jul 20, 2020 at 7:05
  • There's also LINQ method ForEach, which does exactly what it seems to do :) Also there is asynchronous version ForEachAsync Commented Jul 20, 2020 at 7:12
  • @MichalTurczyn Is ForEach in LINQ? I am aware of no such method. Commented Jul 20, 2020 at 8:09
  • 1
    @mjwills you are right. Not LINQ, List<T> has ForEach method. MyList.ToList().ForEach(x=>...) Commented Jul 20, 2020 at 8:22

2 Answers 2

1

In addition to given answers, there's List<T> method (reference) that mimics foreach loop:

EF.GetList(etc).ToList()
    .ForEach(row => row.uploadedAt = MyUtils.ConvertToLocalTime(row.uploadedAtUTC, clientTimezone));

But I don't see any advantage of simplifying things, that are already simple enough.

IMO, too much simplification leads to maintanance horror and reduces readability :)

Sign up to request clarification or add additional context in comments.

2 Comments

LINQ does not have a ForEach. It's a method of the List<T> class.
@JohnWu Thanks, good catch :) corrected that.
0

Reassign the list, ideally without mutating anything. This would be more consistent with the functional programming mindset of LINQ.

IEnumerable<MyObj> myList1 = EF.GetList(etc);
IEnumerable<MyObj> myList2 = myList1.Select( x => 
        new MyObj 
        {
            uploadedAt = MyUtils.ConvertToLocalTime(x.uploadedAtUTC, x.clientTimezone),
            anyOtherFields = x.anyOtherFields
        }
    ).ToList();

If constructing a MyObj is complicated and/or you really need to mutate it, you can also do this:

IEnumerable<MyObj> myList = EF.GetList(etc);
myList = myList.Select( x => {
        x.uploadedAt = MyUtils.ConvertToLocalTime(x.uploadedAtUTC, x.clientTimezone);
        return x;
    }).ToList();

But if you're going to do it that way, you may as well use the for loop.

1 Comment

your second answer is what i tried to do myself, but was stuck with returning the whole object (return x in your answer). Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.