当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Loop through multiple lists and save to new list

问题描述:

I have x number of customers. For each customer, I make a database call that returns a List<DateTime>.

Each list can be empty or contain x number of dates.

What I want to achieve is that for each customer, I take the first date (the oldest) in the list, count the number of times that date is in the list and then save that number to a new List<int> RegList.

The first date for each customer becomes the first item in RegList, the second date the second item, etc.

List customer one

{ 2016-02-19 00:00:00, 2016-02-19 00:00:00 }

RegList should now be: { 2 }

List customer two

{ 2016-04-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 should now be: { 2+3, 1, 2, 1, 1 } = { 5, 1, 2, 1, 1 }

After the first customer my RegList looks right: { 2 }.

After the second customer my RegList looks like this: { 5, 3, 4, 3, 3, 2 }, which is wrong. How should I do this?

EDIT: The dates don't matter. The first date for any customer should be added to the first item in Reglist even if the dates are different.

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++;

}

}

网友答案:

Perhaps it is easier to just import all the dates into a list, and do the process afterwards.

Something like this;

//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);
}
网友答案:

(Think doing this database side)

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; }
}
网友答案:

By looking at the value you are expecting from your data after 2nd list iteration i.e.{ 5, 1, 2, 1, 1 } it looks like you want the number of date occurrences to add for each list of customer Dates. One thing that doesn't look right is you are doing insert for replacing values

RegsList.Insert(i, tempNum);

which is wrong because it just pushes the previous value to the next index. try replacing the above mentioned line with this one:

RegsList[i] = tempNum;

See if it works.

网友答案:

Change

 RegsList.Insert(i, tempNum);

with

RegsList[i] = tempNum;

You can either rewrite this in two distinct steps using a 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;
                }
            } 
        }
分享给朋友:
您可能感兴趣的文章:
随机阅读: