using System.Collections.Generic;
public static void Main()
List<Contract> Contracts = new List<Contract>()
ID = 1, LearningPackageID = 2
ID = 2, LearningPackageID = 4
ID = 3, LearningPackageID = 6
ID = 4, LearningPackageID = 8
List<DepositHistory> DepositHistories = new List<DepositHistory>()
CotractID = 1, Value = 100
CotractID = 2, Value = 50
CotractID = 3, Value = 200
List<LearningPackage> LearningPackages = new List<LearningPackage>()
ID = 2, COntractValue = 100
ID = 4, COntractValue = 200
ID = 6, COntractValue = 300
ID = 8, COntractValue = 400
join b in DepositHistories on a.ID equals b.CotractID into e
from f in e.DefaultIfEmpty()join c in LearningPackages on a.LearningPackageID equals c.ID
where g.Sum(x => x == null ? 0 : x.Value) < g.Key.COntractValue || g.Sum(x => x == null ? 0 : x.Value) == 0
ID = g.Key.ID, Value = g.Sum(x => x == null ? 0 : x.Value), ContractValue = g.Key.COntractValue
foreach (var item in result)
public int LearningPackageID
public class DepositHistory
public class LearningPackage