Показать сообщение отдельно
Старый 18.01.2016, 12:05   #1  
ajlewa is offline
ajlewa
Участник
 
11 / 10 (1) +
Регистрация: 10.08.2015
Не получается поставить даты в "0"
Добрый день,
Не совсем уверен, что выбрал правильную тему.
Скорее всего подобные вопросы поднимались ранее, но в поиске не нашел, так что прошу сильно не пинать, если что то было.

Суть Дела:
В СРМ есть дни рождения людей, записанны в беспорядочном формате, т.е. дата и время, время часто разное.
написал маленькое приложение, что бы исправить ситуацию:

Код:
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;

namespace Drop_Dates_To_Zero
{
    class Program
    {
        static void Main(string[] args)
        {
            var connection = CrmConnection.Parse("Url=http://crm/***/; Domain=***; Username=********; Password=********;");
            var service = new OrganizationService(connection);
            var context = new CrmOrganizationServiceContext(connection);
            int count = 0;

            List<Entity> AllPerson = GetAllAccount(service);

            foreach (var person in AllPerson)
            {
                if (person.Attributes.ContainsKey("birthdate"))
                {
                    var olddate = ((DateTime)person.Attributes["birthdate"]).AddHours(4).Date.ToString(CultureInfo.CurrentCulture);
                    (person.Attributes["birthdate"]) = DateTime.Parse(olddate);
                    context.Update(person);
                    context.SaveChanges();
                }
                count++;
                Console.Clear();
                Console.WriteLine("{0} out of {1} done!", count, AllPerson.Count);
            }

            Console.WriteLine("Ok!");
        }

        private static List<Entity> GetAllAccount(IOrganizationService service)
        {

            int i = 0;
            List<Entity> AllAccount = new List<Entity>();

            try
            {

                var _service = service;
                int fetchCount = 5000;
                int pageNumber = 1;
                List<Guid> dicacc = new List<Guid>();
                QueryExpression QE = new QueryExpression();
                QE.ColumnSet = new ColumnSet("contactid","fullname","birthdate");
                QE.EntityName = "contact";


                QE.PageInfo = new PagingInfo();
                QE.PageInfo.Count = fetchCount;
                QE.PageInfo.PageNumber = pageNumber;
                QE.PageInfo.PagingCookie = null;
                while (true)
                {
                    EntityCollection collections = _service.RetrieveMultiple(QE);
                    if (collections.Entities.Count > 0)
                    {

                        foreach (Entity e in collections.Entities)
                        {
                            i++;

                            AllAccount.Add(e);

                        }
                    }

                    if (collections.MoreRecords)
                    {
                        QE.PageInfo.PageNumber++;
                        QE.PageInfo.PagingCookie = collections.PagingCookie;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            catch (Exception ex)
            {

            }
            return AllAccount;
        }

    }
}
приложение пробегает по всем датам, после чего делаю запрос в бд:
Код:
select top 10 birthdate
from FilteredContact
Where birthdate is not null
Вывод:

Код:
2008-06-01 00:00:00.000
2008-09-04 00:00:00.000
1971-01-18 01:00:00.000
1985-03-21 01:00:00.000
1972-07-22 00:00:00.000
1988-09-12 00:00:00.000
1988-04-01 00:00:00.000
1988-08-03 00:00:00.000
1978-02-27 01:00:00.000
1975-04-16 00:00:00.000
Подскажите в чём может быть причина, и как с этим бороться?