Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change foreach to lambda

Tags:

c#

foreach

lambda

I need a help with simpify this statement. How to change foreach to lambda

var r = mp.Call(c => c.GetDataset());   // returns IEnumerable of dataset      
foreach (DatasetUserAppsUsage item in r)
{
   datasetUserAppsUsage.Merge(item.AppsUsageSummary);
}
like image 408
userbb Avatar asked Nov 27 '22 06:11

userbb


2 Answers

lambdas and loops are orthogonal. It is inappropriate to try to change them to brute-force one into the other. That code is fine. Leave it.

You can get .ForEach implementations, but it isn't going to make the code better (in fact, it will be harder to follow, i.e. worse), and it won't be more efficient (in fact, it will be marginally slower, i.e. worse).

like image 82
Marc Gravell Avatar answered Dec 01 '22 00:12

Marc Gravell


You can do the following

r.ToList().ForEach(item => datasetUserAppsUsage.Merge(item.AppsUsageSummary);
like image 34
JaredPar Avatar answered Nov 30 '22 23:11

JaredPar