简体   繁体   中英

How to avoid loop by using LINQ for the following code?

foreach (Feature fe in features)
{
    if (fileNames.Any(file => file.Contains(fe.FeatureName)))
    {
        fe.MatchCount = fe.MatchCount == 0 ? 1 : fe.MatchCount;
    } 
}

You are mutating the object at the end of the loop-variable, so you can't do that (cleanly) in pure LINQ. Just keep the loop; it'll be simpler to understand, but maybe it can be reduced a bit :

var qry = features.Where(fe => fe.MatchCount == 0 &&
           fileNames.Any(file => file.Contains(fe.FeatureName));

foreach (Feature fe in qry) { fe.MatchCount == 1; }
features.Where(f => fileNames.Any(file => file.Contains(f.FeatureName)))
        .ToList()
        .ForEach(x => x.MatchCount = x.MatchCount == 0 ? 1 : x.MatchCount);

Something worth mentioning is that materializing your query to a list, and then iterating over it again with 'ForEach' can be a pretty expensive call if your list is quite large. I would recommend adding the following extension method to give a 'ForEach' method to IEnumerable:

public static void Map<T>(this IEnumerable<T> source, Action<T> func)
{
    foreach (T i in source)
        func(i);
}

I call mine Map, but you can call it ForEach if you so choose. This turns Danny's answer into:

features.Where(f => fileNames.Any(file => file.Contains(f.FeatureName)))
        .Map(x => x.MatchCount = x.MatchCount == 0 ? 1 : x.MatchCount);
Func<Feature, Feature> updateMatchCount = (fe) =>{
  fe.MatchCount = fe.MatchCount == 0 ? 1 : fe.MatchCount;
  return fe;
 };

 var updatedFeatures = from fe in features
      where fileNames.Any(file => file.Contains(fe.FeatureName))
      select updateMatchCount(fe);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM