2016-08-19 132 views
0

我有x个客户。对于每个客户,我进行数据库调用,返回List<DateTime>
每个列表可以为空或包含x个日期。
通过多个列表循环并保存到新列表

我想要实现的是,对于每个客户,我从列表中选取第一个日期(最早的),计算该日期在列表中的次数,然后将该数字保存到新的List<int> RegList

每个客户的第一个日期成为RegList中的第一项,第二日期的第二项,等等

列表顾客一个
{2016年2月19日00:00:00,2016 -02-19 00:00:00}

RegList现在应该:{2}

名单客户2
{2016年4月22日00:00:00,2016-04-22 00:00:00,2016-04-22 00:00:00,2016-04-25 00:00:00,2016-04-26 00:00:00, 2016-04-26 00:00:00,2016-05-02 00:00:00,2016-05-10 00:00:00}

RegList现在应该是:{2 + 3,1,2 ,1,1} = {5,1,2,1,1}

在第一个客户my RegList看起来正确之后:{2}。 第二位顾客my RegList看起来像这样:{5,3,4,3,3,2},这是错误的。我应该怎么做?

编辑:日期并不重要。即使日期不同,任何客户的第一个日期都应添加到Reglist中的第一个项目。

private void CalculateRegs(List<DateTime> eventList) 
{    
    int num = 0;   
    int i = 0; 

    var distinctList = eventList.Distinct().ToList(); 

    foreach (var date in distinctList) 
    { 
     num = eventList.Where(x => x.Equals(date)).Count(); 
     if (RegsList.Count() <= i) 
     { 
      RegsList.Add(num); 
     } 
     else 
     { 
      var tempNum = num + RegsList.ElementAt(i); 
      RegsList.Insert(i, tempNum); 
     } 
     i++; 
    }   
    } 
+1

那岂不是更容易通过简单地使用组? –

+0

来自“列出客户二”的“2 + 3”在哪里?我只看到“2016-04-11 00:00:00”的三个实例。 – arbitrarystringofletters

+0

arbitrarystringofletters - 我做了更新。日期无关紧要,即使日期不同,客户的第一次约会也应始终添加到RegList [0]。 2 + 3来自顾客1和顾客2 3。 – Andy

回答

0

也许只是将所有日期导入到列表中,然后再执行该过程也许比较容易。

类似这样;

//Create a list of all the dates from your customers 
var dates = new List<DateTime>(); 
foreach(var customer in Customers) 
{ 
    List<DateTime> customerDates = database.GetDates(customer); 
    dates.AddRange(customerDates); 
} 

//You can either just take the count of all the dates 
List<int> RegsList = new List<int>(); 
var group = dates.GroupBy(d => d) 
       .Select(group => group.Count()); 

RegsList.AddRange(group); 

//Or put them in some other collection to know which date has which count 
//for example, a Dictionary 
Dictionary<DateTime, int> OtherRegsList = new Dictionary<DateTime, int>(); 
var group2 = dates.GroupBy(d => d) 
       .Select(group => new { Date = group.Key, Count = group.Count() }); 

foreach(var g in group2) 
{ 
    OtherRegsList.Add(g.Date, g.Count); 
} 
+0

谢谢迪翁,我会试试这个! – Andy

0

(想想做这个数据库端)

void Main() 
{ 
    List<Customer> customers = new List<Customer> { 
     new Customer { 
      CustomerId=1, 
      Events = new List<Event> { 
       new Event {EventId=1, CustomerId=1, EventDate=new DateTime(2016,2,19)}, 
       new Event {EventId=2, CustomerId=1, EventDate=new DateTime(2016,2,19)}, 
      } 
     }, 
     new Customer { 
      CustomerId = 2, 
      Events = new List<Event> { 
       new Event {EventId=6, CustomerId=2, EventDate=new DateTime(2016,4,25)}, 
       new Event {EventId=7, CustomerId=2, EventDate=new DateTime(2016,4,25)}, 
       new Event {EventId=3, CustomerId=2, EventDate=new DateTime(2016,4,22)}, 
       new Event {EventId=4, CustomerId=2, EventDate=new DateTime(2016,4,22)}, 
       new Event {EventId=5, CustomerId=2, EventDate=new DateTime(2016,4,22)}, 
       new Event {EventId=8, CustomerId=2, EventDate=new DateTime(2016,4,26)}, 
       new Event {EventId=8, CustomerId=2, EventDate=new DateTime(2016,4,26)}, 
      } 
     }, 
    }; 

    var counts = from c in customers 
       select new { 
        c.CustomerId, 
        Counts = c.Events 
         .GroupBy(e => e.EventDate) 
         .Select(e => new {Date= e.Key,Count= e.Count()}) 
         .OrderBy(e => e.Date) 
       }; 
} 

public class Customer 
{ 
    public int CustomerId { get; set; } 
    public virtual List<Event> Events {get;set;} 
} 

public class Event 
{ 
    public int EventId {get;set;} 
    public DateTime EventDate { get; set; } 
    public int CustomerId { get; set; } 
} 
0

通过查看您从您的数据后,第二次列表进行迭代,即{5,1,2,1,1}看起来期待值就像您希望为每个客户日期列表添加日期事件的数量一样。 一件事,不右看看是您更换值

RegsList.Insert(i, tempNum); 

这是错误的,因为它只是推前值到下一个索引做插入。 尝试与这一个替换上述行:

RegsList[i] = tempNum; 

看看它是否工作。

0

变化

RegsList.Insert(i, tempNum); 

RegsList[i] = tempNum; 

您可以使用GROUPBY在两个不同的步骤改写这个

private void CalculateRegs(List<DateTime> eventList) 
     { 
      Dictionary<DateTime, int> counts = eventList.GroupBy(x => x) 
       .ToDictionary(g => g.Key, g => g.Count()); 

      // merge with reglist 
      for (int i = 0; i < counts.Count; i++) 
      { 
       var el = counts.ElementAt(i); 
       if (RegsList.Count <= i) 
       { 
        RegsList.Add(counts.ElementAt(i).Value); 
       } 
       else 
       { 
        var tempNum = el.Value + RegsList.ElementAt(i); 
        RegsList[i] = tempNum; 
       } 
      } 
     }