Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Adding foreach value into List

Posted on by Microsoft Employee

Hii, i want to retrieve a field value then add into List. I write this following code:

public List<Presid> GetPresalesIdList(string userlogin)
        {
            List<Presid> idsales = new List<Presid>();
            Presid presales = new Presid();
            string cmb = userdomain + userlogin;
            InitializeCRMService(userName, passWord, domain);

            try
            {
                if (!string.IsNullOrEmpty(userlogin))
                {
                    QueryExpression qe = new QueryExpression("systemuser");
                    string[] cols = { "businessunitid", "domainname", "systemuserid" };
                    qe.Criteria = new FilterExpression();
                    qe.Criteria.AddCondition("domainname", ConditionOperator.Equal, cmb);
                    qe.ColumnSet = new ColumnSet(cols);

                    var guid = _service.RetrieveMultiple(qe);

                    userid = ((EntityReference)guid[0].Attributes["businessunitid"]).Id;
                    systemid = (Guid)guid[0].Attributes["systemuserid"];


                    QueryExpression query = new QueryExpression("opportunity");
                    string[] cols2 = { "new_presalesid", "ownerid" };
                    query.Criteria = new FilterExpression();
                    query.Criteria.AddCondition("ownerid", ConditionOperator.Equal, systemid);
                    query.ColumnSet = new ColumnSet(cols2);
                    EntityCollection preid = _service.RetrieveMultiple(query);

                   
                    foreach (Entity enty in preid.Entities)
                    {
                       
                        presales.PresalesID = enty.GetAttributeValue<string>("new_presalesid");     
                        idsales.Add(presales);
                        
                        
                    }

                    return idsales;
                    
                }
            }
            catch (Exception ex)
            {
                
            }
            return idsales;
        }

But, its give me result : 

i:nil="true"


In other word those result mean that no data has added into List. So i check wether my query get data from CRM or not:

string tes = string.Empty;
                    foreach (Entity enty in preid.Entities)
                    {
                       tes = tes + enty.GetAttributeValue<string>("new_presalesid");
                        presales.PresalesID =  tes; 
                        idsales.Add(presales);
                        
                        
                    }


By modified my code like above, i know that my query has data in it. The "tes" string show me there are many record. So i think the problem start when i want to add the data one by one into the List. However, i already browse on the internet how to add value into list. And i think there is no mistake with my code. Could somebody tell me the way to fix this ?

This is a WCF Service, so i have data contract like this:

 [DataContract]
    public class Presid
    {

        [DataMember]
        public string PresalesID
        {
            get;
            set;
        }
    }




*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Adding foreach value into List

    Thanks for your explaination :)

  • Verified answer
    Pramod M Profile Picture
    Pramod M 1,445 on at
    RE: Adding foreach value into List

    Hi Fikri,

    Class is an object. When you instantiate a new object will be created. When you instantiate outside foreach loop it will create one object, and in the loop is assigned to same object till the loop ends. This means that when a loop executes your previous value will be cleared. And in your list you are adding the same object again and again, but the value inside the object will be overwritten each time.

    But when you instantiate inside the loop , new objects will be created for each looping and hence you are assigning your value to this new object. This object is added to your list. So when you access the list it will contain the number of objects of your class which is equal to the count of your loop and each object contains separate value that you assigned in your loop.

    Hope this helps.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Adding foreach value into List

    Hi Pramod,

    You are right, its not like my query can't add data into list. But, its returning null value because the null value is on the last place. After i add : presales = new Presid(); everything going back to normal.

    Would you mind to explain why i need to initiate my class inside foreach ?

  • Verified answer
    Pramod M Profile Picture
    Pramod M 1,445 on at
    RE: Adding foreach value into List

    Hi Fikri,

    You need to initiate your class in for each loop. Otherwise it will set the last value.

    presales = new Presid();

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans