Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6694153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:04:08+00:00 2026-05-26T06:04:08+00:00

I have this foreach -loop: foreach (var classId in m_ClassMappings[taAddressDefinition.Key]) { if(!m_TAAddressDefinitions.ContainsKey(classId)) { m_TAAddressDefinitions.Add(classId,

  • 0

I have this foreach-loop:

foreach (var classId in m_ClassMappings[taAddressDefinition.Key])
{
    if(!m_TAAddressDefinitions.ContainsKey(classId))
    {
        m_TAAddressDefinitions.Add(classId, taAddressDefinition.Value);
    }
}

m_TAAddressDefinitions is an IDictionary where classId is used as the unique Key value.
Resharper suggests to change the if-statement into into a LINQ filter like this:

foreach (var classId in m_ClassMappings[taAddressDefinition.Key].Where(
    classId => !m_TAAddressDefinitions.ContainsKey(classId)))
{
    m_TAAddressDefinitions.Add(classId, taAddressDefinition.Value);
}

I am conserned if this might not work as expected, since the content of the m_TAAddressDefinitions collection is changing inside the loop, which causes the source of the LINQ filter-condition (classId => !m_TAAddressDefinitions.ContainsKey(classId)) to change on the way.

Will this fail if two classId with same value is added inside the loop, or will the LINQ condition be recalculated when values are added to the collection? My original if-statement was intended to not cause exception if the key already exist.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:04:08+00:00Added an answer on May 26, 2026 at 6:04 am

    In this case, the fact that the IEnumerable returned by Where in the refactored version will lazily produce values as it is iterated over achieves what you want. The suggestion that includes the ToList call is not what you want in this case since that would materialize the IEnumerable as a collection and you would be vulnerable to there being duplicate classIds in the m_ClassMappings collection.

    The thing to remember is that the predicate in the Where call, namely classId => !m_TAAddressDefinitions.ContainsKey(classId) will be evaluated for each item as it is produced as a result of iterating over the IEnumerable. So in the version suggested by Resharper, if there were duplicate values in the m_ClassMappings the first one encountered would be added to the m_TAAddressDefinitions but when the next duplicate is reached the Where predicate will return false because the value has been previously added to the Dictionary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: foreach (var item in ListView1.Items) { ListView1.Items.Remove(item); ListView21.Items.Add(item); } the
I have this foreach loop: var includedElements = new HashSet<int>(); foreach(var e in elements)
I have the following code: foreach (var control in this.Controls) { } I want
I have the this code: var options = GetOptions(From, Value, SelectedValue); var stopWatch =
this is my code snippet, where the program doesn't enter the foreach loop: var
I have this foreach (var columnName in columns) { writer.RenderBeginTag(HtmlTextWriterTag.A); writer.AddAttribute(href, null); writer.Write(Delete); writer.RenderEndTag();
I have a foreach loop var axsEntities = GetAxsEntitiesForInvoicing(adapter) .GroupBy(x => x.AccountUsingAccountIdToAccountId); foreach(var gbAccount
I have this code: List<string> lineList = new List<string>(); foreach (var line in theFinalList)
I have this code here, {foreach from=$cart.cartItems item=item name=cart} <div id=cart2Produkt> <p>{if $item.Product.ID} <a
I have this code while($row = mysql_fetch_row($result)) { echo '<tr>'; $pk = $row[0]['ARTICLE_NO']; foreach($row

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.