none
Looping list and setting property value causes an error RRS feed

  • Question

  • Hello everyone,

    This simple code causes strange issues:

    private IEnumerable<CompsDTO> Map(IEnumerable<Company> companiesList) { try { var result = companiesList.Select(c => Map(c)); foreach (CompsDTO c in result) c.HasChildrens = companiesList.Any(cc => cc.ParentId == c.Id); // here code breaks return result; } catch (Exception ex) {

    } return null; }


    Basically I'm just "converting" Company to CompsDTO in my Map method and after that I'm looping that "converted" list to set HasChildrens property because I couldn't set it in Map method cuz Company class does not contain that prop (I want it only in DTO because thats class that user will see). 

    Issue is : Npgsql.NpgsqlOperationInProgressException: A command is already in progress






    • Edited by Billy_1991 Monday, October 7, 2019 7:06 AM
    Sunday, October 6, 2019 6:58 PM

Answers

All replies

  • The statment is questionable,  and the DTO is a simple object that carries data and doesn't keep state or has behavior.
    Sunday, October 6, 2019 7:19 PM
  • Hi Billy_1991,

    Thank you for posting here.

    Based on your description, you want to solve the error when you set property value in the loop.

    Since you don't give the code that how to convert Company to CompanyDTO, I only create two list to test it.

    After testing it, I find that I could not reproduce your problem.

    Code:

     class Program
        {
            static void Main(string[] args)
            {
                var list = new List<Company>() { new Company { ParentCompanyId=001 }, new Company { ParentCompanyId = 002 } };
                var list1 = new List<CompanyDto>() { new CompanyDto { HasChildrens = false, Id = 001 },new CompanyDto { HasChildrens=false,Id=003} };
                Program p = new Program();
                var m = p.Map(list1, list);
    
            }
            private IEnumerable<CompanyDto> Map(IEnumerable<CompanyDto> result,IEnumerable<Company> companies)
            {
                try
                {
    
                    foreach (CompanyDto c in result)
                    {
                        c.HasChildrens = companies.Any(cc => cc.ParentCompanyId == c.Id); 
                    }
              
    
                    return result;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
    
                return null;
            }
        }
    
        public class Company
        { 
         public int ParentCompanyId { get; set; }
        }
        public class CompanyDto
        {
            public bool HasChildrens { get; set; }
            public int Id { get; set; }
        
        }

    Result:

    If the problem still exists, please provide the related code about converting Company to CompanyDTO.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 7, 2019 2:11 AM
    Moderator
  • Maybe you have to execute ToList:

       var result = companies.Select(c => Map(c)).ToList( );

    Monday, October 7, 2019 3:29 AM
  • Maybe you have to execute ToList:

       var result = companies.Select(c => Map(c)).ToList( );

          Looks like it work, how come? :O
    Monday, October 7, 2019 7:04 AM
  • When you do companies.Select(), that starts an exchange with the database server, and sets up a cursor to read the results,  The results are read one at a time in your foreach loop, and the connection to the database remains open until you have consumed all of the records, which doesn't happen until the foreach loop exists.  When you do companies.Any, that also needs to open a connection to the database server to start a new query, but it's still in the middle of the Select.

    When you call ToList, that immediately pulls all of the records from the database server into memory and creates a simple in-memory List from them.  Because you have sucked all of the records from the query, the query is closed, so the Any function can start a new query.

    It would be better if you could figure out how to combine both of those into a single query, to let the database server do the work instead of your code.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, October 7, 2019 7:09 PM
  • Maybe you have to execute ToList:

       var result = companies.Select(c => Map(c)).ToList( );

          Looks like it work, how come? :O
    The ToList() instanced a List<T> object  and loaded the DTO(s) into it.
    Tuesday, October 8, 2019 12:05 AM
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 15, 2019 2:09 AM
    Moderator