c# - Detect repeat and new customers -
i'm trying figure how write linq query return 2 lists of customers have checked in first time , didn't check in on given date
cusomters - name (nvarchar) - checkindate (datetime)
i can write 2 separate queries, possible use single query?
this return 1 list of 2 kinds of customer want:
var result = customers.groupby(x=>x.name) .where(g=>g.count()==1 || !g.any(x=>x.checkindate == yourdate)) .selectmany(x=>x);
if want 2 lists, think work:
var result = customers.groupby(x=> customers.count(a=>a.name == x.name) == 1 ? 1 : 2) .select(g=> g.key == 1 ? g.tolist() : g.where(x=>!g.where(a=>a.checkindate == yourdate) .any(a=>a.name == x.name)).tolist()); //to customers have first checked in var firstcheckedin = result[0]; //to customers didn't check in on given date var notcheckedin = result[1] //or var result = customers.groupby(x=> { var c = customers.where(a=>a.name == x.name); return c.count() == 1 ? 1 : !c.any(a=>a.checkindate == yourdate) ? 2 : 3; }) .select(g=> g.tolist()).tolist();
Comments
Post a Comment