多表连接查询
List<R_EquipmentContractInfo> objlist = new List<R_EquipmentContractInfo>(); //所有的合同 var contractList = dc_ModelData.Contract_BaseInfor.Where(p => p.BidSectionRowGuid == bidRowGuid).ToList(); if (contractList == null || contractList.Count() == 0) { return objlist; } //合同所有的物料 var allMT = from g in contractList join m in dc_ModelData.MaterialType.Where(p => p.MonomerProject == bidRowGuid && p.Count > 0) on g.RowGuid equals m.ContractRowGuid select m; var efiles = (from g in allMT join h in dc_ModelData.EquipmentEFile_Category.Where(p => p.BidSectionRowGuid == bidRowGuid && p.Title == "族文件") on g.RowGuid equals h.MaterialDetailRowGuid into ecGroup from h2 in ecGroup join k in dc_ModelData.EquipmentEFile.Where(p => p.BidSectionRowGuid == bidRowGuid && p.IsMaxVersion == true) on h2.RowGuid equals k.ParentRowGuid into efGroup select new { mt=g,ec=ecGroup,ef=efGroup}).Distinct(); int mtCount = allMT.Count(); int ecount = efiles.Count(); foreach (Contract_BaseInfor ec in contractList) { R_EquipmentContractInfo obj = new R_EquipmentContractInfo(); obj.rowGuid = ec.RowGuid; obj.name = ec.Name; //总数量 List<MaterialType> mmdList = allMT.Where(p=>p.ContractRowGuid == ec.RowGuid).ToList(); if (mmdList == null || mmdList.Count == 0) { obj.completeCount = 0; obj.unCompleteCount = 0; objlist.Add(obj); continue; } List<MaterialType> pmmdlist = (from g in mmdList join h in efiles on g.RowGuid equals h.mt.RowGuid select g).Distinct().ToList(); if (pmmdlist == null || pmmdlist.Count == 0) { obj.completeCount = 0; } else { obj.completeCount = pmmdlist.Count; } obj.unCompleteCount = mmdList.Count - obj.completeCount; objlist.Add(obj); } return objlist;