Stranger Stranger - 4 years ago 64
C# Question

Count Dates which a specific Customer does not have a Shop in it

Here is an example Data Table:

ID Amount Date
--------------------------
1 3.000 2016/1/1
2 4.000 2016/1/1
1 6.000 2017/1/1
1 3.000 2018/1/1
3 2.000 2019/1/1


I need to count Dates which a specific Customers does not have a Shop in it.
for example
ID 2
does not have a shop in
2017/1/1
and
2018/1/1
, so the count will be
2
. and the count for customer
ID 3
will be
3
because He does not have a shop in
2016/1/1
and
2017/1/1
and
2018/1/1


I think I should use grouping but do not know how to count which I want for a specific ID!

orders.GroupBy(x => x.Date)......???

Answer Source

Assuming you have list of objects:

// number of all different dates
var datesCount = list.Select(i => i.Date).Distinct().Count(); 

int customerId = 2;

//number of dates with customer shopping
var customerDatesCount = list.Where(i => i.ID == customerId).Select(i => i.Date).Distinct().Count();

var nonShoppingDatesCount = datesCount - customerDatesCount;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download