N. Nazarenko
N. Nazarenko

Reputation: 28

List and dictionary in one linq query(not subquery) c#

I have this code:

for (int i = 0; i < _smartBoostItems.Count; i++)
{
    foreach (var condition in _levelConditionsDict)
    {
        if (_smartBoostItems[i].progression_to_finish >= level.PercentsLeftToLevelCompleteWhileTurnType
            && _smartBoostItems[i].condition 
            && _smartBoostItems[i].condition_id == condition.Key)
        {
            _availibleSmartBoosts.Add(_smartBoostItems[i]);
        }
    }
}    

_smartBoostItems - it's a List<SmartBoostLibItem>
_levelConditionsDict - it's a Dictionary<int, int>

How can I convert this code to a linq query? Or maybe it will be worse? I don't like this nested loops, there are not too much elements. There will be about 500 iterations.

Upvotes: 1

Views: 110

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460148

You can use this query which is more efficient:

_availibleSmartBoosts = _smartBoostItems
    .Where(i=> i.progression_to_finish >= level.PercentsLeftToLevelCompleteWhileTurnType
          && i.condition && _levelConditionsDict.ContainsKey(i.condition_id))
    .ToList();

Upvotes: 2

Related Questions