Shawn
Shawn

Reputation: 33

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;
    } 
}

Upvotes: 3

Views: 1347

Answers (4)

diceguyd30
diceguyd30

Reputation: 2752

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);

Upvotes: 1

revington
revington

Reputation: 160

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);

Upvotes: 0

Cheng Chen
Cheng Chen

Reputation: 43523

features.Where(f => fileNames.Any(file => file.Contains(f.FeatureName)))
        .ToList()
        .ForEach(x => x.MatchCount = x.MatchCount == 0 ? 1 : x.MatchCount);

Upvotes: 3

Marc Gravell
Marc Gravell

Reputation: 1062770

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; }

Upvotes: 9

Related Questions