web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Adding foreach value into List

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Verified answer
    Pramod M Profile Picture
    1,445 on at

    Hi Fikri,

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

    presales = new Presid();

  • Community Member Profile Picture
    on at

    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
    1,445 on at

    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
    on at

    Thanks for your explaination :)

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans