Linq Remove from list based on other table
One way to do it when you have two lists (repYearlyData and otherdata)
foreach (var yd in repYearlyData)
{
for(int d=0;d<otherdata.Count;d++)
{
if (yd.AssignedRep == otherdata[d].AssignedRep)
{
otherdata.RemoveAt(d);
}
}
}
public List<TeamAverages> GetRepMonthlyAverages()
{
int tday = DateTime.Now.Day;
int mnth = DateTime.Now.Month;
int yr = DateTime.Now.Year;
var otherdata = (from c in db.calldetail_viw
join w in db.UserWorkgroups on c.LocalUserId equals w.UserId into workGroups
from ug in workGroups.Where(w => !w.WorkGroup.Contains("Team")).Where(w => !w.WorkGroup.Contains("Email"))
.Where(w => !w.WorkGroup.Contains("Dummy")).DefaultIfEmpty()
where c.ConnectedDate.Value.Year == yr
&& c.InteractionType == 5
group c by new { c.LocalUserId } into grouped
select new {
AssignedRep = grouped.Key.LocalUserId,
YearlyAverageResponse = grouped.Average(x => x.CallDurationSeconds.Value),
YearlyEmailCount = grouped.Count(),
FullName = "Other"
}).ToList();
var repDailyData = db.calldetail_viw
.Where(d => d.ConnectedDate.Value.Year == yr)
.Where(d => d.ConnectedDate.Value.Month == mnth)
.Where(d => d.ConnectedDate.Value.Day == tday)
.Where(d => d.InteractionType == 5)
.Join(db.UserWorkgroups, d => d.LocalUserId, w => w.UserId, (d, w) => new { calldetail_viw = d, UserWorkgroup = w }).Where(o => o.UserWorkgroup.WorkGroup.Contains("Team")).Where(o => !o.UserWorkgroup.WorkGroup.Contains("Dummy")).Where(o => !o.UserWorkgroup.WorkGroup.Contains("Email"))
.Join(db.Individuals, w => w.UserWorkgroup.UserId, i => i.ICUserID, (d, i) => new { calldetail_viw = d, Individual = i }).Where(p => !p.Individual.JobTitle.Contains("Supervisor"))
.GroupBy(x => new { x.calldetail_viw.calldetail_viw.LocalUserId, x.Individual.FirstName, x.Individual.LastName, x.calldetail_viw.UserWorkgroup.WorkGroup })
.Select(y => new
{
AssignedRep = y.Key.LocalUserId,
TodaysAverageResponse = y.Average(x => x.calldetail_viw.calldetail_viw.CallDurationSeconds.Value),
TodaysEmailCount = y.Count(),
// FullName = y.Key.FirstName + " " + y.Key.LastName
}).OrderBy(d => d.AssignedRep).ToList();
//(from z in otherdata where z.AssignedRep !=repDailyData.Any(p=>p.AssignedRep));
// var filteredList = otherdata.Where(a => repDailyData.All(b => b.AssignedRep != a.AssignedRep)).Dump();
var repMonthlyData = db.calldetail_viw
.Where(d => d.ConnectedDate.Value.Year == yr)
.Where(d => d.ConnectedDate.Value.Month == mnth)
.Where(d => d.InteractionType == 5)
.Join(db.UserWorkgroups, d => d.LocalUserId, w => w.UserId, (d, w) => new { calldetail_viw = d, UserWorkgroup = w }).Where(o => o.UserWorkgroup.WorkGroup.Contains("Team")).Where(o => !o.UserWorkgroup.WorkGroup.Contains("Dummy")).Where(o => !o.UserWorkgroup.WorkGroup.Contains("Email"))
.Join(db.Individuals, w => w.UserWorkgroup.UserId, i => i.ICUserID, (d, i) => new { calldetail_viw = d, Individual = i }).Where(p => !p.Individual.JobTitle.Contains("Supervisor"))
.GroupBy(x => new { x.calldetail_viw.calldetail_viw.LocalUserId, x.Individual.FirstName, x.Individual.LastName, x.calldetail_viw.UserWorkgroup.WorkGroup })
.Select(y => new
{
AssignedRep = y.Key.LocalUserId,
MonthlyAverageResponse = y.Average(x => x.calldetail_viw.calldetail_viw.CallDurationSeconds.Value),
MonthlyEmailCount = y.Count(),
// FullName = y.Key.FirstName + " " + y.Key.LastName
}).Where(d => d.MonthlyEmailCount > 0).OrderBy(d => d.AssignedRep).ToList();
var repYearlyData = db.calldetail_viw
.Where(d => d.ConnectedDate.Value.Year == yr)
.Where(d => d.InteractionType == 5)
.Join(db.UserWorkgroups, d => d.LocalUserId, w => w.UserId, (d, w) => new { calldetail_viw = d, UserWorkgroup = w }).Where(o => o.UserWorkgroup.WorkGroup.Contains("Team")).Where(o => !o.UserWorkgroup.WorkGroup.Contains("Dummy")).Where(o => !o.UserWorkgroup.WorkGroup.Contains("Email"))
.Join(db.Individuals, w => w.UserWorkgroup.UserId, i => i.ICUserID, (d, i) => new { calldetail_viw = d, Individual = i }).Where(p => !p.Individual.JobTitle.Contains("Supervisor"))
.GroupBy(x => new { x.calldetail_viw.calldetail_viw.LocalUserId, x.Individual.FirstName, x.Individual.LastName, x.calldetail_viw.UserWorkgroup.WorkGroup })
.Select(y => new
{
AssignedRep = y.Key.LocalUserId,
YearlyAverageResponse = y.Average(x => x.calldetail_viw.calldetail_viw.CallDurationSeconds.Value),
YearlyEmailCount = y.Count(),
FullName = y.Key.FirstName + " " + y.Key.LastName
}).OrderBy(d => d.AssignedRep).ToList();
foreach (var yd in repYearlyData)
{
for(int d=0;d<otherdata.Count;d++)
{
if (yd.AssignedRep == otherdata[d].AssignedRep)
{
otherdata.RemoveAt(d);
}
}
}
var finalData = (from co in otherdata
group co by new { co.FullName } into othGroup
select new
{
AssignedRep = "" ,
YearlyAverageResponse = othGroup.Average(x => x.YearlyAverageResponse),
YearlyEmailCount = othGroup.Sum(x=>x.YearlyEmailCount),
FullName = othGroup.Key.FullName
}).ToList();
var totalYearlyData = repYearlyData.Concat(finalData);
var returnList = new List<TeamAverages>();
foreach (var row in totalYearlyData)
{
var t = new TeamAverages();
t.FullName = row.FullName;
t.AssignedRep = row.AssignedRep;
t.bestrep = 0;
t.YearlyEmailCount = row.YearlyEmailCount;
t.YearlyAverageResponse = row.YearlyAverageResponse;
foreach (var d in repDailyData.Where(p => p.AssignedRep == row.AssignedRep))
if (row.AssignedRep == d.AssignedRep)
{
t.TodaysEmailCount = d.TodaysEmailCount;
t.TodaysAverageResponse = d.TodaysAverageResponse;
}
foreach (var d in repMonthlyData.Where(p => p.AssignedRep == row.AssignedRep))
if (row.AssignedRep == d.AssignedRep)
{
t.MonthlyEmailCount = d.MonthlyEmailCount;
t.MonthlyAverageResponse = d.MonthlyAverageResponse;
}
returnList.Add(t);
}
return returnList.ToList();
}
foreach (var yd in repYearlyData)
{
for(int d=0;d<otherdata.Count;d++)
{
if (yd.AssignedRep == otherdata[d].AssignedRep)
{
otherdata.RemoveAt(d);
}
}
}
var result = peopleList2.Where(p => !peopleList1.Any(p2 => p2.ID == p.ID));
public List<TeamAverages> GetRepMonthlyAverages()
{
int tday = DateTime.Now.Day;
int mnth = DateTime.Now.Month;
int yr = DateTime.Now.Year;
var otherdata = (from c in db.calldetail_viw
join w in db.UserWorkgroups on c.LocalUserId equals w.UserId into workGroups
from ug in workGroups.Where(w => !w.WorkGroup.Contains("Team")).Where(w => !w.WorkGroup.Contains("Email"))
.Where(w => !w.WorkGroup.Contains("Dummy")).DefaultIfEmpty()
where c.ConnectedDate.Value.Year == yr
&& c.InteractionType == 5
group c by new { c.LocalUserId } into grouped
select new {
AssignedRep = grouped.Key.LocalUserId,
YearlyAverageResponse = grouped.Average(x => x.CallDurationSeconds.Value),
YearlyEmailCount = grouped.Count(),
FullName = "Other"
}).ToList();
var repDailyData = db.calldetail_viw
.Where(d => d.ConnectedDate.Value.Year == yr)
.Where(d => d.ConnectedDate.Value.Month == mnth)
.Where(d => d.ConnectedDate.Value.Day == tday)
.Where(d => d.InteractionType == 5)
.Join(db.UserWorkgroups, d => d.LocalUserId, w => w.UserId, (d, w) => new { calldetail_viw = d, UserWorkgroup = w }).Where(o => o.UserWorkgroup.WorkGroup.Contains("Team")).Where(o => !o.UserWorkgroup.WorkGroup.Contains("Dummy")).Where(o => !o.UserWorkgroup.WorkGroup.Contains("Email"))
.Join(db.Individuals, w => w.UserWorkgroup.UserId, i => i.ICUserID, (d, i) => new { calldetail_viw = d, Individual = i }).Where(p => !p.Individual.JobTitle.Contains("Supervisor"))
.GroupBy(x => new { x.calldetail_viw.calldetail_viw.LocalUserId, x.Individual.FirstName, x.Individual.LastName, x.calldetail_viw.UserWorkgroup.WorkGroup })
.Select(y => new
{
AssignedRep = y.Key.LocalUserId,
TodaysAverageResponse = y.Average(x => x.calldetail_viw.calldetail_viw.CallDurationSeconds.Value),
TodaysEmailCount = y.Count(),
// FullName = y.Key.FirstName + " " + y.Key.LastName
}).OrderBy(d => d.AssignedRep).ToList();
//(from z in otherdata where z.AssignedRep !=repDailyData.Any(p=>p.AssignedRep));
// var filteredList = otherdata.Where(a => repDailyData.All(b => b.AssignedRep != a.AssignedRep)).Dump();
var repMonthlyData = db.calldetail_viw
.Where(d => d.ConnectedDate.Value.Year == yr)
.Where(d => d.ConnectedDate.Value.Month == mnth)
.Where(d => d.InteractionType == 5)
.Join(db.UserWorkgroups, d => d.LocalUserId, w => w.UserId, (d, w) => new { calldetail_viw = d, UserWorkgroup = w }).Where(o => o.UserWorkgroup.WorkGroup.Contains("Team")).Where(o => !o.UserWorkgroup.WorkGroup.Contains("Dummy")).Where(o => !o.UserWorkgroup.WorkGroup.Contains("Email"))
.Join(db.Individuals, w => w.UserWorkgroup.UserId, i => i.ICUserID, (d, i) => new { calldetail_viw = d, Individual = i }).Where(p => !p.Individual.JobTitle.Contains("Supervisor"))
.GroupBy(x => new { x.calldetail_viw.calldetail_viw.LocalUserId, x.Individual.FirstName, x.Individual.LastName, x.calldetail_viw.UserWorkgroup.WorkGroup })
.Select(y => new
{
AssignedRep = y.Key.LocalUserId,
MonthlyAverageResponse = y.Average(x => x.calldetail_viw.calldetail_viw.CallDurationSeconds.Value),
MonthlyEmailCount = y.Count(),
// FullName = y.Key.FirstName + " " + y.Key.LastName
}).Where(d => d.MonthlyEmailCount > 0).OrderBy(d => d.AssignedRep).ToList();
var repYearlyData = db.calldetail_viw
.Where(d => d.ConnectedDate.Value.Year == yr)
.Where(d => d.InteractionType == 5)
.Join(db.UserWorkgroups, d => d.LocalUserId, w => w.UserId, (d, w) => new { calldetail_viw = d, UserWorkgroup = w }).Where(o => o.UserWorkgroup.WorkGroup.Contains("Team")).Where(o => !o.UserWorkgroup.WorkGroup.Contains("Dummy")).Where(o => !o.UserWorkgroup.WorkGroup.Contains("Email"))
.Join(db.Individuals, w => w.UserWorkgroup.UserId, i => i.ICUserID, (d, i) => new { calldetail_viw = d, Individual = i }).Where(p => !p.Individual.JobTitle.Contains("Supervisor"))
.GroupBy(x => new { x.calldetail_viw.calldetail_viw.LocalUserId, x.Individual.FirstName, x.Individual.LastName, x.calldetail_viw.UserWorkgroup.WorkGroup })
.Select(y => new
{
AssignedRep = y.Key.LocalUserId,
YearlyAverageResponse = y.Average(x => x.calldetail_viw.calldetail_viw.CallDurationSeconds.Value),
YearlyEmailCount = y.Count(),
FullName = y.Key.FirstName + " " + y.Key.LastName
}).OrderBy(d => d.AssignedRep).ToList();
foreach (var yd in repYearlyData)
{
for(int d=0;d<otherdata.Count;d++)
{
if (yd.AssignedRep == otherdata[d].AssignedRep)
{
otherdata.RemoveAt(d);
}
}
}
var finalData = (from co in otherdata
group co by new { co.FullName } into othGroup
select new
{
AssignedRep = "" ,
YearlyAverageResponse = othGroup.Average(x => x.YearlyAverageResponse),
YearlyEmailCount = othGroup.Sum(x=>x.YearlyEmailCount),
FullName = othGroup.Key.FullName
}).ToList();
var totalYearlyData = repYearlyData.Concat(finalData);
var returnList = new List<TeamAverages>();
foreach (var row in totalYearlyData)
{
var t = new TeamAverages();
t.FullName = row.FullName;
t.AssignedRep = row.AssignedRep;
t.bestrep = 0;
t.YearlyEmailCount = row.YearlyEmailCount;
t.YearlyAverageResponse = row.YearlyAverageResponse;
foreach (var d in repDailyData.Where(p => p.AssignedRep == row.AssignedRep))
if (row.AssignedRep == d.AssignedRep)
{
t.TodaysEmailCount = d.TodaysEmailCount;
t.TodaysAverageResponse = d.TodaysAverageResponse;
}
foreach (var d in repMonthlyData.Where(p => p.AssignedRep == row.AssignedRep))
if (row.AssignedRep == d.AssignedRep)
{
t.MonthlyEmailCount = d.MonthlyEmailCount;
t.MonthlyAverageResponse = d.MonthlyAverageResponse;
}
returnList.Add(t);
}
return returnList.ToList();
}
Comments