Accepted answer

Thanks to Ivan in the comments I came up with this solution:

var fourYearsAgo = DateTime.Now.AddYears(-4).Year;

var dataWithoutGrouping = from m in MainData
                              where m.CID == "334r" && m.STMTDT.Value.Year > fourYearsAgo
                              join a in Adjustments
                                on new {m.STMTDT, m.Stmtno} equals new {a.STMTDT, a.Stmtno} into grp
                              from ja in grp.DefaultIfEmpty()
                              select new {
                                                Dt = m.STMTDT,
                                                No = m.Stmtno,
                                                Fee = m.PayAmount,
                                                Adjustment = ja.Amount

    var data = (from b in dataWithoutGrouping
                group b by new {b.Dt, b.No }into grp
                select new {
                   StatmentFee = grp.Sum(x => x.Fee),
                   StatementAdjustments = grp.Sum(x => x.Adjustment),
                   StatementDate = grp.Key.Dt,
                   StatementNo = grp.Key.No

I just needed to use grp.Key as the FirstOrDefault() was creating a select for each record.

Also, here is the answer in VB(I translated my question into C# as VB isn't that popular here and I translated it back) if anyone needs it:

 Dim fourYearsAgo = DateTime.Now().AddYears(-4).Year

    Dim dataWithoutGrouping = From m In dbContext.MainDatas
                              Where m.CID = "334r" AndAlso m.STMTDT.Value.Year > fourYearsAgo
                              Group Join a In dbContext.Adjustments
                                On New With {m.STMTDT, m.stmtno} Equals New With {a.STMTDT, a.Stmtno} Into Group
                              From ja In Group.DefaultIfEmpty()
                              Select New With {
                                                .Dt = m.STMTDT,
                                                .No = m.stmtno,
                                                .Fee = m.PayAmount,
                                                .Adjustment = ja.Amount

    Dim data = (From b In dataWithoutGrouping
                Group b By grpKeys = New With {b.Dt, b.No} Into Group
                Select New With {
                   .StatmentFee = Group.Sum(Function(x) x.Fee),
                   .StatementAdjustments = Group.Sum(Function(x) x.Adjustment),
                   .StatementDate = grpKeys.Dt,
                   .StatementNo = grpKeys.No

It generates this SQL:

DECLARE @p0 VarChar(1000) = '334r'
DECLARE @p1 Int = 2014
-- EndRegion
SELECT SUM([t2].[PayAmount]) AS [StatmentFee], SUM([t2].[value]) AS [StatementAdjustments], [t2].[STMTDT] AS [StatementDate], [t2].[stmtno] AS [StatementNo]
    SELECT [t0].[STMTDT], [t0].[stmtno], [t0].[PayAmount], [t1].[Amount] AS [value], [t0].[CID]
    FROM [MainData] AS [t0]
    LEFT OUTER JOIN [Adjustments] AS [t1] ON ([t0].[STMTDT] = [t1].[STMTDT]) AND ([t0].[stmtno] = [t1].[Stmtno])
    ) AS [t2]
WHERE ([t2].[CID] = @p0) AND (DATEPART(Year, [t2].[STMTDT]) > @p1)
GROUP BY [t2].[STMTDT], [t2].[stmtno]


This is the Result i got:

If you have already your EF for MainData and Adjustment

I created a list:

    List<MainData> mdata = new List<MainData>();
    List<Adjustments> adj = new List<Adjustments>();
    List<Result> resFinal = new List<Result>();

and the LINQ

var gety = DateTime.Now.AddYears(-4);
            var res = from h in mdata
                      join j in adj
                      on h.StmtNo equals j.StmtNo
                      select new Result { StmtDate = h.StmtDate, StmtNo = h.StmtNo, Fee = h.Fee, Adj = j.Adj, Total = (h.Fee * j.Adj) };
            resFinal = res.Cast<Result>().Where(x=>x.StmtDate > gety).ToList();

The Final Class Holder as a Result

 public class Result
        public DateTime StmtDate { get; set; }
        public int StmtNo { get; set; }
        public double Fee { get; set; }
        public double Adj { get; set; }
        public double Total { get; set; }

Related Articles