How to write two left joins in the linq - c#

var CCEScholasticTests = db.CCEScholasticTests.Where(x => x.CCEvaluationID == CCEValuationID && x.SubjectID == SubjectID && x.ClassID == ClassID && (x.BranchSectionID == BranchSectionID || BranchSectionID == 0) && x.languageTypeSubjectID == languageTypeSubjectID && (x.BranchID == BranchID || x.BranchID == 0) && x.IsOnlyGrade == false).Select(x => x).ToList();
var res = (from a in CCEScholasticTests
join b in db2.CCESubjectSkills on new { key1 = a.CCESubjectSkillID } equals new { key1 = b.CCESubjectSkillID } into join1
from joinRes in join1.DefaultIfEmpty(new CCESubjectSkill())
join c in db2.CCEScholasticSkillsMasters on new { key = joinRes.CCEScholasticSkillMasterID } equals new { key = c.CCEScholasticSkillMasterID } into join2
from joinRes2 in join2.DefaultIfEmpty(new CCEScholasticSkillsMaster())
select new EvaluationBluk
{
BranchSectionID = a.BranchSectionID,
CCEScholasticTestID = a.CCEScholasticTestID,
CCEScholasticTestName = a.CCEScholasticTestName,
//CCESubjectSkillName = joinRes.CCESubjectSkillName,
CCESubjectSkillID = a.CCESubjectSkillID,
CCEvaluationID = a.CCEvaluationID,
MaxMarks = a.MaxMarks,
SubjectID = a.SubjectID,
TestDate = a.TestDate,
MarksEntryLastDate = a.MarksEntryLastDate,
//CCEScholasticSkillMasterID = joinRes.CCEScholasticSkillMasterID,
//CCEScholasticSkillName = joinRes2.CCEScholasticSkillName
}).ToList();
how to write multiple left joins in linq.. the below linq query i am writing based on this sp
CREATE procedure [dbo].[GetClassCCEScholasticTestsOnlyMarksTest]
(
#BranchSectionID int,
#CCEvaluationID int,
#SubjectID int,
#ClassID int ,
#languageTypeSubjectID int ,
#BranchID int
)
as
select c1.BranchSectionID,c1.CCEScholasticTestID,c1.CCEScholasticTestName,s1.CCESubjectSkillName,
c1.CCESubjectSkillID,c1.CCEvaluationID,c1.MaxMarks,c1.SubjectID,convert(varchar,c1.TestDate,101) as TestDate,convert(date,c1.MarksEntryLastDate,101)as MarksEntryLastDate
,isnull(s1.CCEScholasticSkillMasterID,-1) as CCEScholasticSkillMasterID ,cm.CCEScholasticSkillName
from dbo.CCEScholasticTests c1
left join CCESubjectSkills s1 on s1.CCESubjectSkillID=c1.CCESubjectSkillID
left join CCEScholasticSkillsMaster cm on cm.CCEScholasticSkillMasterID=s1.CCEScholasticSkillMasterID
where c1.CCEvaluationID=#CCEvaluationID and c1.SubjectID=#SubjectID
and c1.ClassID=#ClassID and (c1.BranchSectionID=#BranchSectionID or c1.BranchSectionID=0) and c1.languageTypeSubjectID =#languageTypeSubjectID
and (c1.BranchID=#BranchID or c1.BranchID=0)and c1.IsOnlyGrade=0
order by c1.CCEScholasticTestID asc
in sql it throws 3 rows but in linq it throws 1 row only... what wrong in my code

var result=CCEScholasticTests.join(db2.CCESubjectSkills ,o=>o.CCESubjectSkillID, od=>od.CCESubjectSkillID,(o, od)=> new
{
BranchSectionID = o.BranchSectionID,
CCEScholasticTestID = o.CCEScholasticTestID,
CCEScholasticTestName = o.CCEScholasticTestName,
CCESubjectSkillID = o.CCESubjectSkillID,
CCEvaluationID = o.CCEvaluationID,
MaxMarks = o.MaxMarks,
SubjectID = o.SubjectID,
TestDate = o.TestDate,
MarksEntryLastDate = o.MarksEntryLastDate,
CCEScholasticSkillMasterID = od.CCEScholasticSkillMasterID
}).join(db2.CCEScholasticSkillsMasters, s=>s.CCEScholasticSkillMasterID = t.CCEScholasticSkillMasterID, t=>t.CCEScholasticSkillMasterID,(s,t)=> new
{
BranchSectionID = o.BranchSectionID,
CCEScholasticTestID = o.CCEScholasticTestID,
CCEScholasticTestName = o.CCEScholasticTestName,
CCESubjectSkillID = o.CCESubjectSkillID,
CCEvaluationID = o.CCEvaluationID,
MaxMarks = o.MaxMarks,
SubjectID = o.SubjectID,
TestDate = o.TestDate,
MarksEntryLastDate = o.MarksEntryLastDate,
CCESubjectSkillName = t.CCESubjectSkillName,
CCEScholasticSkillMasterID = t.CCEScholasticSkillMasterID,
CCEScholasticSkillName = t.CCEScholasticSkillName
}).tolist();
I dont know the exact relation between the table but, i have assumed and written query. hope it helps u

Related

How to assign stored procedure result to a entity inside select statement in entity framework

I want to assign GetMajorMileStone(projecttask.ProjectTaskId) result to MajorMilestone.
I tried but getting following error:
LINQ to Entities does not recognize the method
'System.Data.Objects.ObjectResult1[.Poco.GetMajorMileStone_Result]
GetMajorMileStone(System.Nullable1[System.Guid])' method, and this
method cannot be translated into a store expression.'
Here is my code:
public ProjectsPayload GetProjectSchedule(int projectid, bool includeArchived, DateTime startDate, DateTime endDate, int userId)
{
using (var db = new Entities())
{
try
{
db.CommandTimeout = 1200;
var query = from project in db.Project.Where(t => (t.Active || t.TempActive) && t.ProjectId == projectid)
join UP in db.User_X_Project on project.ProjectId equals UP.ProjectId
where (UP.UserId == userId && UP.Active)
orderby (project.Priority ?? int.MaxValue)
orderby (project.ProjectTitle)
select new
{
Project = project,
ProjectTask = from projecttask in project.ProjectTask.Where(t => t.Active && (
(includeArchived == true && t.TaskStatusId == (int?)TaskStatus.Archived) ||
(includeArchived == false && t.TaskStatusId != (int?)TaskStatus.Archived))
|| t.TaskStatusId != (int?)TaskStatus.Planned)
join schedule in project.ProjectTask.SelectMany(p => p.ProjectTaskSchedule) on projecttask.ProjectTaskId equals schedule.ProjectTaskId
join daily in db.ProjectTaskSchedule.SelectMany(p => p.DailyStatus) on schedule.ProjectTaskScheduleId equals daily.ProjectTaskScheduleId
where schedule.Active && daily.Active && projecttask.Active && schedule.ResourceId == userId && (
(EntityFunctions.TruncateTime(daily.Date) >= EntityFunctions.TruncateTime(startDate.Date) &&
EntityFunctions.TruncateTime(daily.Date) <= EntityFunctions.TruncateTime(endDate.Date))
)
orderby schedule.StartDate
select new
{
ProjectTask = projecttask,
ProjectTaskSchedule = from projecttaskschedule in projecttask.ProjectTaskSchedule.Where(t => t.Active && t.ResourceId == userId)
select new
{
ProjectTaskSchedule = projecttaskschedule,
DailyStatus = projecttaskschedule.DailyStatus.Where(t => t.Active),
},
CritiCality = from cr in db.CritiCality.Where(ts => ts.ProjectTaskId == projecttask.ProjectTaskId) select cr,
MMDetails = from mm in db.MMDetails.Where(ts => ts.ProjectTaskId == projecttask.ProjectTaskId) select mm,
MajorMilestone = db.GetMajorMileStone(projecttask.ProjectTaskId).FirstOrDefault(),
}
};
var materialized = query.AsEnumerable();
var result = materialized.Select(t => new ProjectsPayload
{
ProjectId = t.Project.ProjectId,
ProjectTitle = t.Project.ProjectTitle,
Priority = t.Project.Priority,
ProjectDescription = t.Project.ProjectDescription,
ProjectTask = t.Project.ProjectTask.Select(x => new ProjectTaskPayload
{
Duration = x.Duration,
Hours = x.Hours,
IsOngoing = x.IsOngoing,
IsSummaryTask = x.IsSummaryTask,
Priority = x.Priority,
ParentTaskId = x.ParentTaskId,
ProjectId = x.ProjectId,
ProjectTaskId = x.ProjectTaskId,
TaskAcceptanceId = x.TaskAcceptanceId,
TaskStatusId = x.TaskStatusId,
TaskTitle = x.TaskTitle,
TaskTypeId = x.TaskTypeId,
IsMileStone = x.IsMileStone,
IsTimeAwayTask = x.IsTimeAwayTask,
AutoSize = x.AutoSize,
IsArchivedTasksInSummary = x.IsArchivedTasksInSummary,
IsASAP = x.IsASAP,
IsAutoCompleteEnable = x.IsAutoCompleteEnable,
IsSharedDiffSchedules = x.IsSharedDiffSchedules,
LongDescription = x.LongDescription,
OwnerId = x.OwnerId,
ShowInSummaryTask = x.ShowInSummaryTask,
SubTypeID = x.SubTypeID,
MMDetails1 = x.MMDetails1.Select(MD => new MMDetailsPayload { MajorMilestoneId = MD.MajorMilestoneId, ProjectTaskId = MD.ProjectTaskId, MMDetailsId = MD.MMDetailsId, Slack = MD.Slack }),
ProjectTaskSchedule = x.ProjectTaskSchedule.Select(a => new ProjectsTaskSchedulePayload
{
ProjectTaskScheduleId = a.ProjectTaskScheduleId,
StartDate = a.StartDate,
EndDate = a.EndDate,
ProjectTaskId = a.ProjectTaskId,
ResourceId = a.ResourceId,
ArchiveEndDate = a.ArchiveEndDate,
ArchiveStartDate = a.ArchiveStartDate,
IsSharedTask = a.IsSharedTask,
TimeUnitId = a.TimeUnitId,
DailyStatus = a.DailyStatus.Select(Ds => new DailyStatusPayload
{
Active = Ds.Active,
ActualHours = Ds.ActualHours,
DailyStatusId = Ds.DailyStatusId,
Date = Ds.Date,
ProjectTaskScheduleId = Ds.ProjectTaskScheduleId,
IsCloseOutDay = Ds.IsCloseOutDay,
Priority = Ds.Priority
})
}).ToList(),
CritiCality = x.CritiCality.Select(c => new CriticalityPayload { CriticalityId = c.CriticalityId, CriticalityTypeId = c.CriticalityTypeId, ProjectTaskId = c.ProjectTaskId }).ToList(),
}).ToList()
}).FirstOrDefault();
return result;
}
catch (EntityException ex)
{
if (ex.Message == connectionException)
throw new FaultException(dbException);
else
throw new FaultException(ex.Message);
}
}
}
My result is like this, I want all entities(Criticality,MMDetails and MajorMileStone), Not only MajorMileStone
MajorMileStone Result
I separate multiple simple query for testing. Please try to execute below two query and check whether successful or not.
// First query to get project
var query1 = from project in db.Project.Where(t => (t.Active || t.TempActive) && t.ProjectId == projectid)
join UP in db.User_X_Project on project.ProjectId equals UP.ProjectId
where (UP.UserId == userId && UP.Active)
orderby (project.Priority ?? int.MaxValue)
orderby (project.ProjectTitle)
.Select new { project = project }.ToList();
// Second query to get projecttask from query1.project and execute store procedure
var query2 = from projecttask in query1.project.ProjectTask.Where(t => t.Active && (
(includeArchived == true && t.TaskStatusId == (int?)TaskStatus.Archived) ||
(includeArchived == false && t.TaskStatusId != (int?)TaskStatus.Archived)) ||
t.TaskStatusId != (int?)TaskStatus.Planned)
join schedule in query1.project.ProjectTask.SelectMany(p => p.ProjectTaskSchedule) on projecttask.ProjectTaskId equals schedule.ProjectTaskId
join daily in db.ProjectTaskSchedule.SelectMany(p => p.DailyStatus) on schedule.ProjectTaskScheduleId equals daily.ProjectTaskScheduleId
where schedule.Active && daily.Active && projecttask.Active && schedule.ResourceId == userId && (
(EntityFunctions.TruncateTime(daily.Date) >= EntityFunctions.TruncateTime(startDate.Date) &&
EntityFunctions.TruncateTime(daily.Date) <= EntityFunctions.TruncateTime(endDate.Date))
)
orderby schedule.StartDate
.Select new
{
MajorMilestone = db.GetMajorMileStone(projecttask.ProjectTaskId).FirstOrDefault(),
}.ToList();
Let me know it has result or not. In the meantime you can check this "Method cannot be translated into a store expression"
It has link to article about what not to do with linq. Nested Linq like this is best to avoid and better to split up the query. If it affects the execution time, it better to execute raw sql.

How to get the group sum in my complex linq query

below I have listed a linq query that works properly in my asp.net.mvc web app.
In addition I would like to group over 'allowance.ParameterId' in order to get the group sum for 'allowance.Freight (instead of multiple records for the given key).
var query = from ara in aras
join company in companies on ara.Id equals company.ARAId
join wasteWater in wasteWaters on company.Id equals wasteWater.CompanyId
join allowance in allowances on wasteWater.Id equals allowance.WasteWaterID
join parameter in parameters on allowance.ParameterId equals parameter.Id into JoinedParameterAllowance
from parameter in JoinedParameterAllowance.DefaultIfEmpty()
where company.Activ == true && company.End == null && company.Template == false
&& wasteWater.End == null
select new FreightSummaryViewModel
{
AraName = ara.Name,
AraId = ara.Id,
AllowedParameter = parameter.Name,
AllowedFreight = allowance.Freight
};
I have tried to insert 'group ...' but failed to get it right.
Could someone help me please to set up the proper syntax?
Thank you in advance, Manu
I have little idea about relations in your database, so I improvised...
// Some dummy data to play with
var aras = Enumerable.Range(0, 5).Select(i => new { Id = i, Name = "Ara" + i });
var companies = Enumerable.Range(0, 15).Select(i => new { Id = i, ARAId = i % 5, Activ = true, End = (DateTime?)null, Template = false });
var wasteWaters = Enumerable.Range(0, 35).Select(i => new { Id = i, CompanyId = i / 15, End = (DateTime?)null });
var allowances = Enumerable.Range(0, 70).Select(i => new { Id = i, WasteWaterID = i, ParameterId = i % 4, Freight = i * 1000 });
var parameters = Enumerable.Range(0, 4).Select(i => new { Id = i, Name = "Parameter" + i });
And this is what I believe you looked for:
var query =
from ara in aras
join company in companies on ara.Id equals company.ARAId
join wasteWater in wasteWaters on company.Id equals wasteWater.CompanyId
join allowance in allowances on wasteWater.Id equals allowance.WasteWaterID
join parameter in parameters on allowance.ParameterId equals parameter.Id
into JoinedParameterAllowance
// from parameter in JoinedParameterAllowance.DefaultIfEmpty()
where true
&& company.Activ == true
&& company.End == null
&& company.Template == false
&& wasteWater.End == null
group allowance by new
{
AraName = ara.Name,
AraId = ara.Id,
ParameterId = allowance.ParameterId
} into myGroup
select new //FreightSummaryViewModel
{
AraName = myGroup.Key.AraName,
AraId = myGroup.Key.AraId,
AllowedParameter = myGroup.Key.ParameterId,
AllowedFreight = myGroup.Sum(g => g.Freight)
};

How to check any value from Decimal array exist in a decimal field of a table?

Let's I have a decimal array BOOK_NO[]. I have existing query like this:
PrintOrderList = (
from t1 in db.DIS_SET_PRINT_REQ
join t2 in db.RND_BOOK on t1.F_BOOK_NO equals t2.BOOK_NO into first_joined
where (t1.IS_EXIST == 1 &&
t1.F_COMPANY_NO == Company_No &&
#book_no.(t1.F_BOOK_NO))
from dept_data in first_joined.DefaultIfEmpty()
select new DIS_SET_PRINT_REQ_R_SINGLE_MODEL
{
BOOK_NO = dept_data.BOOK_NO,
BOOK_NAME = dept_data.BOOK_NAME,
EDITION_NAME = dept_data.RND_SET_BOOK_EDITION.BOOK_EDITION_NAME,
FISCAL_YEAR = t1.SNS_SET_FISCAL_YEAR.FISCAL_YEAR_NAME,
ACADEMIC_LEVEL = dept_data.RND_SET_BOOK_ACA_LEVEL.BOOK_ACA_LEVEL_NAME,
PRINT_QUANTITY = t1.BOOK_QTY,
PRINT_REQ_NO = t1.PRINT_REQ_NO,
PRINT_SEQ = (db.DIS_SET_PRINT_REQ_DTL.Where(x => x.F_PRINT_REQ_NO == t1.PRINT_REQ_NO)
.Where(x => x.IS_EXIST == 1)
.OrderBy(x => x.PRINT_REQ_DTL_NO)),
FISCAL_YEAR_NO = t1.F_FISCAL_YEAR_NO,
EDITION_NO = t1.RND_BOOK.F_BOOK_EDITION_NO,
ACADEMIC_LEVEL_NO = dept_data.RND_SET_BOOK_ACA_LEVEL.BOOK_ACA_LEVEL_NO,
CATEGORY_NO = dept_data.F_BOOK_CATEGORY_NO
}).ToList();
Here I need to put a condition that BOOK_NO[] contains any t1.F_BOOK_NO
Use .Contains on the BOOK_NO[] to check that the t1.BOOK_NO is in it
PrintOrderList = (
from t1 in db.DIS_SET_PRINT_REQ
join t2 in db.RND_BOOK on t1.F_BOOK_NO equals t2.BOOK_NO into first_joined
where t1.IS_EXIST == 1 &&
t1.F_COMPANY_NO == Company_No &&
book_no.Contains(t1.F_BOOK_NO)
from dept_data in first_joined.DefaultIfEmpty()
select new DIS_SET_PRINT_REQ_R_SINGLE_MODEL
{
BOOK_NO = dept_data.BOOK_NO,
BOOK_NAME = dept_data.BOOK_NAME,
EDITION_NAME = dept_data.RND_SET_BOOK_EDITION.BOOK_EDITION_NAME,
FISCAL_YEAR = t1.SNS_SET_FISCAL_YEAR.FISCAL_YEAR_NAME,
ACADEMIC_LEVEL = dept_data.RND_SET_BOOK_ACA_LEVEL.BOOK_ACA_LEVEL_NAME,
PRINT_QUANTITY = t1.BOOK_QTY,
PRINT_REQ_NO = t1.PRINT_REQ_NO,
PRINT_SEQ = (db.DIS_SET_PRINT_REQ_DTL.Where(x => x.F_PRINT_REQ_NO == t1.PRINT_REQ_NO)
.Where(x => x.IS_EXIST == 1)
.OrderBy(x => x.PRINT_REQ_DTL_NO)),
FISCAL_YEAR_NO = t1.F_FISCAL_YEAR_NO,
EDITION_NO = t1.RND_BOOK.F_BOOK_EDITION_NO,
ACADEMIC_LEVEL_NO = dept_data.RND_SET_BOOK_ACA_LEVEL.BOOK_ACA_LEVEL_NO,
CATEGORY_NO = dept_data.F_BOOK_CATEGORY_NO
}).ToList();

Transforming T-SQL Query into C# LINQ with joins on multiple conditions and also grouping on multiple conditions

First I want to say hello, I'm new to this site ;-)
My problem is to transform the following sql-query into a c# linq-query.
( I HAVE searched hard for an existing answer but I'm not able to combine the solution for
the joins on multiple conditions and the grouping / counting ! )
The sql-query :
DECLARE #datestart AS DATETIME
DECLARE #dateend AS DATETIME
SET #datestart = '01.04.2014'
SET #dateend = '30.04.2014'
SELECT md1.value AS [controller],md2.value AS [action], COUNT(md2.value) AS [accesscount], MAX(re.TIMESTAMP) AS [lastaccess] FROM recorderentries AS re
INNER JOIN messagedataentries AS md1 ON re.ID = md1.recorderentry_id AND md1.position = 0
INNER JOIN messagedataentries AS md2 ON re.ID = md2.recorderentry_id AND md2.position = 1
WHERE re.TIMESTAMP >= #datestart AND re.TIMESTAMP <= #dateend
AND re.messageid IN ('ID-01','ID-02' )
GROUP BY md1.value,md2.value
ORDER BY [accesscount] DESC
Any suggestions are welcome ...
What i have so far is this :
var _RecorderActionCalls = (from r in _DBContext.RecorderEntries
join m1 in _DBContext.MessageDataEntries on
new {
a = r.ID,
b = 0
} equals new {
a = m1.ID,
b = m1.Position
}
join m2 in _DBContext.MessageDataEntries on
new {
a = r.ID,
b = 0
} equals new {
a = m2.ID,
b = m2.Position
}
where r.TimeStamp >= StartDate & r.TimeStamp <= EndDate & (r.MessageID == "VAREC_100_01" | r.MessageID == "VAAUTH-100.01")
group r by new { md1 = m1.Value, md2 = m2.Value } into r1
select new { controller = r1.Key.md1, action = r1.Key.md2, count = r1.Key.md2.Count() }).ToList();
But this throws an exception ( translated from german ) :
DbExpressionBinding requires an input expression with a Listing Result Type ...
UPDATE : Back with headache ... ;-)
I found a solution to my problem :
var _RecorderActionCalls = _DBContext.RecorderEntries
.Where(r => r.TimeStamp >= StartDate & r.TimeStamp <= EndDate & (r.MessageID == "VAREC_100_01" | r.MessageID == "VAAUTH-100.01"))
.GroupBy(g => new { key1 = g.MessageData.FirstOrDefault(md1 => md1.Position == 0).Value, key2 = g.MessageData.FirstOrDefault(md2 => md2.Position == 1).Value })
.Select(s => new {
ControllerAction = s.Key.key1 + " - " + s.Key.key2,
Value = s.Count(),
Last = s.Max(d => d.TimeStamp)
}).ToList();
With this syntax it works for me. Thank you for thinking for me :-)
Something like that:
List<string> messageIdList = new List<string> { "ID-01", "ID-02" };
from re in RecorderEntries
from md1 in MessageDataEntries
from md2 in MessageDataEntries
where re.ID = md1.recorderEntry_id && md1.position == 0
where re.ID = md2.recorderEntry_id && md2.position == 1
where idList.Contains(re.messageid)
let joined = new { re, md1, md2 }
group joined by new { controller = joined.md1.value, action = joined.md2.value } into grouped
select new {
controller = grouped.Key.controller,
action = grouped.Key.action,
accesscount = grouped.Where(x => x.md2.value != null).Count(),
lastaccess = grouped.Max(x => x.re.TimeStamp) }

LINQ-to-SQL - Group By

I have the following LINQ-to-SQL code
var customerList = from cm in dc.ConsignmentMarginBreakdowns
join tm in dc.ConsignmentTripBreakdowns on new { Depot = cm.Depot, TripNumber = cm.TripNumber, TripDate = cm.TripDate } equals new { Depot = tm.Depot, TripNumber = tm.TripNumber, TripDate = tm.TripDate }
join sl in dc.SageAccounts on new { LegacyID = cm.Customer, Customer = true } equals new { LegacyID = sl.LegacyID, Customer = sl.Customer }
join ss in dc.SageAccounts on sl.ParentAccount equals ss.ID
where (tm.DeliveryDate >= dateRange1.FromDate && tm.DeliveryDate <= dateRange1.ToDate) || (dateRange2.FromDate != null && (tm.DeliveryDate >= dateRange2.FromDate && tm.DeliveryDate <= dateRange2.ToDate))
where tm.Depot == depotLetter
group cm by new { ss.Name, ss.ID } into cmg
select new
{
CustomerID = cmg.Key.ID,
CustomerName = cmg.Key.Name,
Sales1 = cmg.Where(a => a.DeliveryDate >= dateRange1.FromDate && a.DeliveryDate <= dateRange1.ToDate).Sum(a => a.TripSalesTotal),
Sales2 = dateRange2.FromDate != null ? tmg.Where(a => a.DeliveryDate >= dateRange2.FromDate && a.DeliveryDate <= dateRange2.ToDate).Sum(a => a.TripSalesTotal) : 0.00m
};
The problem I have is that the 'DeliveryDate' from 'tm' is not in the scope of the query when I select. Any ideas how I can get this value in my query?
Thanks
Just create another anonymous type
group new { cm, tm } by new { ss.Name, ss.ID } into cmg
so your query would look like this:
var customerList = from cm in dc.ConsignmentMarginBreakdowns
join tm in dc.ConsignmentTripBreakdowns on new { Depot = cm.Depot, TripNumber = cm.TripNumber, TripDate = cm.TripDate } equals new { Depot = tm.Depot, TripNumber = tm.TripNumber, TripDate = tm.TripDate }
join sl in dc.SageAccounts on new { LegacyID = cm.Customer, Customer = true } equals new { LegacyID = sl.LegacyID, Customer = sl.Customer }
join ss in dc.SageAccounts on sl.ParentAccount equals ss.ID
where (tm.DeliveryDate >= dateRange1.FromDate && tm.DeliveryDate <= dateRange1.ToDate) || (dateRange2.FromDate != null && (tm.DeliveryDate >= dateRange2.FromDate && tm.DeliveryDate <= dateRange2.ToDate))
where tm.Depot == depotLetter
group new { cm, tm } by new { ss.Name, ss.ID } into cmg
select new
{
CustomerID = cmg.Key.ID,
CustomerName = cmg.Key.Name,
Sales1 = cmg.Where(a => a.tm.DeliveryDate >= dateRange1.FromDate && a.tm.DeliveryDate <= dateRange1.ToDate).Sum(a => a.cm.TripSalesTotal),
Sales2 = dateRange2.FromDate != null ? tmg.Where(a => a.tm.DeliveryDate >= dateRange2.FromDate && a.tm.DeliveryDate <= dateRange2.ToDate).Sum(a => a.cm.TripSalesTotal) : 0.00m
};

Categories

Resources