locked
Extracting an entity class from a list. RRS feed

  • Question

  • Hi,

    Please would someone help me.  I have a list of a class (QaUserEntity) in a list called targetList. I want to loop through the list and extract each item individually so that I can get to the elements of the class so that I can populate a listbox with a single string.

    This is what I have so far... The problem lies in the line of code 'userEntity = targetList.IndexOf(i);'  The i is highlighted and it tells me   -  Error CS1503 Argument 1: cannot convert from 'int' to 'QIAS.Business.Entities.QaUserEntity'


            private void LoadUserNames(List<QaUserEntity> targetList)
            {
                int listCount;
                string resultString = "";

                listCount = targetList.Count;
                for (int i = 0; i < listCount; i++)
                {
                    QaUserEntity userEntity = new QaUserEntity();
                    userEntity = targetList.IndexOf(i);
                    resultString = userEntity.FirstName + " " + userEntity.Surname + " " + userEntity.LogonName + " " +  userEntity.UserID;

                    lstResult.Items.Add(resultString);
                }
            }

    Many Thanks,  

    Sue


    • Edited by Soozi-Wong Thursday, December 1, 2016 9:40 AM
    Thursday, December 1, 2016 9:37 AM

Answers

  • Hi,

    the IndexOf() method gives you only the integer index of the object, not the List object itself. Instead, try

    for (int i = 0; i < listCount; i++)
    {
        resultString = targetList[i].FirstName + " " + targetList[i].Surname + " " + targetList[i].LogonName + " " +  targetList[i].UserID;
    
        lstResult.Items.Add(resultString);
    }
    
    

    If targetList is a generic List of QaUserEntity objects you can retrieve each element of that list by its index.

    wizend

    • Marked as answer by Soozi-Wong Thursday, December 1, 2016 12:06 PM
    Thursday, December 1, 2016 11:22 AM

All replies

  • Hi,

    the IndexOf() method gives you only the integer index of the object, not the List object itself. Instead, try

    for (int i = 0; i < listCount; i++)
    {
        resultString = targetList[i].FirstName + " " + targetList[i].Surname + " " + targetList[i].LogonName + " " +  targetList[i].UserID;
    
        lstResult.Items.Add(resultString);
    }
    
    

    If targetList is a generic List of QaUserEntity objects you can retrieve each element of that list by its index.

    wizend

    • Marked as answer by Soozi-Wong Thursday, December 1, 2016 12:06 PM
    Thursday, December 1, 2016 11:22 AM
  • Brilliant! It worked! 

    Many thanks,

    Sue

    Thursday, December 1, 2016 12:08 PM