简体   繁体   中英

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.

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

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