Juanjo Juanjo - 1 year ago 86
C# Question

How to convert an expression to linq with nested foreach when the first element is being modified

I am having trouble on how to convert this to Linq. Any help will be greatly appreciated.

foreach (var stat in stats)
foreach (var record in worked)
if (stat.Name != record.Name) continue;
stat.WorkedToday = record.RecordCount;

Answer Source

Please find an answer in .NET FIDDLE I hope this will help

//be careful method 'First' can throw an exception if there is no such element in sequence
  .ForEach(s => s.WorkedToday = worked.First(w => w.Name == s.Name).RecordCount);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download