我有x个客户。对于每个客户,我进行一次返回List<DateTime>
的数据库调用
每个列表可以为空或包含x个日期。
我想要实现的是,对于每个客户,我采用列表中的第一个日期(最旧的),计算该日期在列表中的次数,然后将该数字保存到新的{{1} }。
每个客户的第一个日期成为List<int> RegList
中的第一个日期,第二个日期成为第二个项目等等。
列出客户
{2016-02-19 00:00:00,2016-02-19 00:00:00}
RegList
现在应为:{2}
列出客户二
{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
现在应该是:{2 + 3,1,2,1,1} = {5,1,2,1,1}}
第一位客户RegList
看起来正确后:{2}。
在第二个客户之后我的RegList
看起来像这样:{5,3,4,3,3,2},这是错误的。我该怎么做?
编辑:日期无关紧要。即使日期不同,也应将RegList
中的第一个日期添加到任何客户的第一个日期。
Reglist
答案 0 :(得分: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);
}
答案 1 :(得分: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; }
}
答案 2 :(得分:0)
通过查看第二个列表迭代后您期望从数据中获得的值,即{5,1,2,1,1},您似乎希望为每个客户日期列表添加日期出现次数。 一件看起来不正确的事情是你正在插入替换值
RegsList.Insert(i, tempNum);
这是错误的,因为它只是将前一个值推送到下一个索引。 尝试用这个替换上面提到的行:
RegsList[i] = tempNum;
看看它是否有效。
答案 3 :(得分: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;
}
}
}