Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CRM编码逻辑错误_C#_Logic_Dynamics Crm_Crm - Fatal编程技术网

C# CRM编码逻辑错误

C# CRM编码逻辑错误,c#,logic,dynamics-crm,crm,C#,Logic,Dynamics Crm,Crm,这实际上与特定错误无关;我的代码没有做它应该做的事情 我有一个代码块(foreach循环),它初始化另一个类中的方法(“getCustomerByGuid()”)。此方法应将一个项目客户集合(CRM中的列表)添加到另一个客户集合。循环时,它应使用所有~2500个值填充客户集合 问题是,循环完成后,customer集合中只填充了一个customer—列表中要添加到集合中的最后一个customer。我试过几种不同的方法,但没有成功。。因为MS CRM只显示集合中的一个客户(即营销列表,它是活动的一部

这实际上与特定错误无关;我的代码没有做它应该做的事情

我有一个代码块(foreach循环),它初始化另一个类中的方法(“getCustomerByGuid()”)。此方法应将一个项目客户集合(CRM中的列表)添加到另一个客户集合。循环时,它应使用所有~2500个值填充客户集合

问题是,循环完成后,customer集合中只填充了一个customer—列表中要添加到集合中的最后一个customer。我试过几种不同的方法,但没有成功。。因为MS CRM只显示集合中的一个客户(即营销列表,它是活动的一部分,等等)

这是我的密码。让我知道你的想法。谢谢

这是正在运行的主Program.cs代码:

private static void doCRMWork()
    {
        try
        {
            CRMConnectionHelper.Authenticate();

            Console.WriteLine("Begin.");

            //queryCrm();

            const string f = @"C:\WindowsApps\CRM\crm_interface3\data\CRMGuids_HM2011_v2.txt";

            List<string> guids = new List<string>();

            using (StreamReader r = new StreamReader(f))
            {
                string line;
                while ((line = r.ReadLine()) != null)
                {
                    guids.Add(line);
                }
            }

            CustomerCollection customers = new CustomerCollection();

            foreach (string s in guids)
            {
                customers.GetCustomersByGuid(s);
                Console.WriteLine("Customer added");

                //Overwriting the customer collection every loop iteration...
            }

            Campaign cmpn = new Campaign();
            cmpn.CreateCampaign("2011 Test3 Holiday Mailing");

            Console.WriteLine("Campaign and activity created. Press enter to continue.");
            Console.ReadLine();

            cmpn.AddCustomersToCampaign(customers);
            Console.WriteLine("Created marketing list and added customers to campaign.");
            Console.WriteLine("End.");
            Console.ReadLine();
        }
下面是GetCrmCustomers方法的代码:

    private static CustomerCollection GetCrmCustomers(FilterExpression filter, Customer.CustomerTypes customerType)
    {
        CustomerCollection customers = new CustomerCollection();
        if (customerType == Customer.CustomerTypes.Contact)
        {
            QueryExpression query = new QueryExpression();
            query.ColumnSet = new AllColumns();
            query.EntityName = EntityName.contact.ToString();
            if (filter != null)
            {
                query.Criteria = filter;
            }
            BusinessEntityCollection bc = new BusinessEntityCollection();
            try 
            { 
                bc = CRMInterface.crmService.RetrieveMultiple(query); 
            }
            catch 
            { 
            }
            if (bc.BusinessEntities != null)
            {
                for (int i = 0; i < bc.BusinessEntities.Length; i++)
                {
                    //I think BusinessEntities.Length is 1 because of 'new string[] {GUID}' -- one element.
                    Console.WriteLine("i is: " + i + "and bc.BusinessEntities.Length is:  " + bc.BusinessEntities.Length);
                    Customer customer = new Customer();
                    customer.CustomerType = Customer.CustomerTypes.Contact;
                    customer.GUID = ((contact)bc.BusinessEntities[i]).contactid.Value.ToString();
                    customer.Firstname = ((contact)bc.BusinessEntities[i]).firstname == null ? "" : ((contact)bc.BusinessEntities[i]).firstname;
                    customer.Middlename = ((contact)bc.BusinessEntities[i]).middlename == null ? "" : ((contact)bc.BusinessEntities[i]).middlename;
                    customer.Lastname = ((contact)bc.BusinessEntities[i]).lastname == null ? "" : ((contact)bc.BusinessEntities[i]).lastname;
                    customer.Suffix = ((contact)bc.BusinessEntities[i]).suffix == null ? "" : ((contact)bc.BusinessEntities[i]).suffix;
                    customer.Email = ((contact)bc.BusinessEntities[i]).emailaddress1 == null ? "" : ((contact)bc.BusinessEntities[i]).emailaddress1.Trim().ToLower();
                    customer.Donotbulkemail = ((contact)bc.BusinessEntities[i]).donotbulkemail == null ? false : ((contact)bc.BusinessEntities[i]).donotbulkemail.Value;
                    customers.Add(customer); //***
                    //So customers is the collection that is added to the end of the list each time.

                    //This loop is only run once in this class, but is run over and 
                    //over again in the program class.
                    //It might be 

                }
            }
        }
        else
        {
            QueryExpression query = new QueryExpression();
            query.ColumnSet = new AllColumns();
            query.EntityName = EntityName.account.ToString();
            query.Criteria = filter;
            BusinessEntityCollection bc = new BusinessEntityCollection();
            try 
            { 
                bc = CRMInterface.crmService.RetrieveMultiple(query); 
            }
            catch 
            { 
            }

            if (bc.BusinessEntities != null)
            {
                for (int i = 0; i < bc.BusinessEntities.Length; i++)
                {
                    Customer customer = new Customer();
                    customer.CustomerType = Customer.CustomerTypes.Account;
                    customer.GUID = ((account)bc.BusinessEntities[i]).accountid.Value.ToString();
                    customer.Name = ((account)bc.BusinessEntities[i]).name == null ? "" : ((account)bc.BusinessEntities[i]).name;
                    customer.Email = ((account)bc.BusinessEntities[i]).emailaddress1 == null ? "" : ((account)bc.BusinessEntities[i]).emailaddress1.Trim().ToLower();
                    customer.Donotbulkemail = ((account)bc.BusinessEntities[i]).donotbulkemail == null ? false : ((account)bc.BusinessEntities[i]).donotbulkemail.Value;
                    customers.Add(customer);
                }
            }
        }
        return customers;
    }
私有静态CustomerCollection GetCrmCustomers(FilterExpression筛选器,Customer.CustomerTypes customerType)
{
CustomerCollection客户=新CustomerCollection();
if(customerType==Customer.CustomerTypes.Contact)
{
QueryExpression query=新建QueryExpression();
query.ColumnSet=newallcolumns();
query.EntityName=EntityName.contact.ToString();
if(过滤器!=null)
{
query.Criteria=filter;
}
BusinessEntityCollection bc=新的BusinessEntityCollection();
尝试
{ 
bc=CRMInterface.crmService.RetrieveMultiple(查询);
}
抓住
{ 
}
if(bc.BusinessEntities!=null)
{
for(int i=0;i

谢谢…

您在
getCustomerByGuid()
中的第一行是
this.Clear(),即在每次调用该方法时(即在循环的每次迭代中)删除所有客户。然后,该方法中的最后一行
this.AddRange
,但这只会添加当前客户


我认为
这个.Clear()doCRMWork

中,code>应该在
foreach(guids中的字符串)前面。。。可能是这样。Clear()。不知道为什么我到现在才注意到。我会测试它,让你知道会发生什么。。。罗拉:就是这样。。我在发布这篇文章后不久就注意到了。哦。。。谢谢
    private static CustomerCollection GetCrmCustomers(FilterExpression filter, Customer.CustomerTypes customerType)
    {
        CustomerCollection customers = new CustomerCollection();
        if (customerType == Customer.CustomerTypes.Contact)
        {
            QueryExpression query = new QueryExpression();
            query.ColumnSet = new AllColumns();
            query.EntityName = EntityName.contact.ToString();
            if (filter != null)
            {
                query.Criteria = filter;
            }
            BusinessEntityCollection bc = new BusinessEntityCollection();
            try 
            { 
                bc = CRMInterface.crmService.RetrieveMultiple(query); 
            }
            catch 
            { 
            }
            if (bc.BusinessEntities != null)
            {
                for (int i = 0; i < bc.BusinessEntities.Length; i++)
                {
                    //I think BusinessEntities.Length is 1 because of 'new string[] {GUID}' -- one element.
                    Console.WriteLine("i is: " + i + "and bc.BusinessEntities.Length is:  " + bc.BusinessEntities.Length);
                    Customer customer = new Customer();
                    customer.CustomerType = Customer.CustomerTypes.Contact;
                    customer.GUID = ((contact)bc.BusinessEntities[i]).contactid.Value.ToString();
                    customer.Firstname = ((contact)bc.BusinessEntities[i]).firstname == null ? "" : ((contact)bc.BusinessEntities[i]).firstname;
                    customer.Middlename = ((contact)bc.BusinessEntities[i]).middlename == null ? "" : ((contact)bc.BusinessEntities[i]).middlename;
                    customer.Lastname = ((contact)bc.BusinessEntities[i]).lastname == null ? "" : ((contact)bc.BusinessEntities[i]).lastname;
                    customer.Suffix = ((contact)bc.BusinessEntities[i]).suffix == null ? "" : ((contact)bc.BusinessEntities[i]).suffix;
                    customer.Email = ((contact)bc.BusinessEntities[i]).emailaddress1 == null ? "" : ((contact)bc.BusinessEntities[i]).emailaddress1.Trim().ToLower();
                    customer.Donotbulkemail = ((contact)bc.BusinessEntities[i]).donotbulkemail == null ? false : ((contact)bc.BusinessEntities[i]).donotbulkemail.Value;
                    customers.Add(customer); //***
                    //So customers is the collection that is added to the end of the list each time.

                    //This loop is only run once in this class, but is run over and 
                    //over again in the program class.
                    //It might be 

                }
            }
        }
        else
        {
            QueryExpression query = new QueryExpression();
            query.ColumnSet = new AllColumns();
            query.EntityName = EntityName.account.ToString();
            query.Criteria = filter;
            BusinessEntityCollection bc = new BusinessEntityCollection();
            try 
            { 
                bc = CRMInterface.crmService.RetrieveMultiple(query); 
            }
            catch 
            { 
            }

            if (bc.BusinessEntities != null)
            {
                for (int i = 0; i < bc.BusinessEntities.Length; i++)
                {
                    Customer customer = new Customer();
                    customer.CustomerType = Customer.CustomerTypes.Account;
                    customer.GUID = ((account)bc.BusinessEntities[i]).accountid.Value.ToString();
                    customer.Name = ((account)bc.BusinessEntities[i]).name == null ? "" : ((account)bc.BusinessEntities[i]).name;
                    customer.Email = ((account)bc.BusinessEntities[i]).emailaddress1 == null ? "" : ((account)bc.BusinessEntities[i]).emailaddress1.Trim().ToLower();
                    customer.Donotbulkemail = ((account)bc.BusinessEntities[i]).donotbulkemail == null ? false : ((account)bc.BusinessEntities[i]).donotbulkemail.Value;
                    customers.Add(customer);
                }
            }
        }
        return customers;
    }