For such of requirement you have to "group" data in a different way. Please, check out below example:
DataTable VisitDetails = new DataTable();
VisitDetails.Columns.Add(new DataColumn("VisitId", typeof(int)));
VisitDetails.Columns.Add(new DataColumn("visitCode", typeof(string)));
VisitDetails.Columns.Add(new DataColumn("visitDate", typeof(DateTime)));
VisitDetails.Rows.Add(new object[]{1, "V001", new DateTime(2015,12,15)});
DataTable PatientBill = new DataTable();
PatientBill.Columns.Add(new DataColumn("PatientBillId", typeof(int)));
PatientBill.Columns.Add(new DataColumn("VisitId", typeof(int)));
PatientBill.Columns.Add(new DataColumn("BillCode", typeof(string)));
PatientBill.Columns.Add(new DataColumn("PatientPayable", typeof(int)));
PatientBill.Columns.Add(new DataColumn("CompanyPayable", typeof(int)));
PatientBill.Rows.Add(new object[]{1, 1, "B001", 100, 50});
PatientBill.Rows.Add(new object[]{2, 1, "B002", 80, 40});
DataTable PatientBillDetails = new DataTable();
PatientBillDetails.Columns.Add(new DataColumn("PBillDetailsId", typeof(int)));
PatientBillDetails.Columns.Add(new DataColumn("PatientBIllId", typeof(int)));
PatientBillDetails.Columns.Add(new DataColumn("Gross", typeof(int)));
PatientBillDetails.Columns.Add(new DataColumn("Discount", typeof(int)));
PatientBillDetails.Columns.Add(new DataColumn("NetPayable", typeof(int)));
PatientBillDetails.Rows.Add(new object[]{1, 1, 100, 25, 75});
PatientBillDetails.Rows.Add(new object[]{2, 1, 100, 25, 75});
PatientBillDetails.Rows.Add(new object[]{3, 2, 80, 20, 60});
PatientBillDetails.Rows.Add(new object[]{4, 2, 80, 20, 60});
var result2 = (from PB in PatientBill.AsEnumerable()
select new
{
VisitId = PB.Field<int>("VisitId"),
VisitCode = (from VD in VisitDetails.AsEnumerable()
where VD.Field<int>("VisitId")==PB.Field<int>("VisitId")
select VD.Field<string>("visitCode")).First(),
BillCode = PB.Field<string>("BillCode"),
PatientPayable = PB.Field<int>("PatientPayable"),
CompanyPayable = PB.Field<int>("CompanyPayable"),
NetPayable = (from PBD in PatientBillDetails.AsEnumerable()
where PBD.Field<int>("PatientBIllId")==PB.Field<int>("PatientBIllId")
select PBD.Field<int>("NetPayable")).Sum(),
}).ToList();
var result1 = (from r in result2
group r by r.VisitCode into g
select new
{
VisitCode = g.Key,
PatientPayable = g.Sum(x=>x.PatientPayable),
CompanyPayable = g.Sum(x=>x.CompanyPayable),
NetPayable = g.Sum(x=>x.NetPayable)
}).ToList();
Result #1
VisitCode PatientPayable CompanyPayable NetPayable
V001 180 90 270
Resutl #2
VisitId VisitCode BillCode PatientPayable CompanyPayable NetPayable
1 V001 B001 100 50 150
1 V001 B002 80 40 120