Datatable C# - 如何根据 ID 从一个表中获取详细记录及其关联的另一表中的子记录
Posted
技术标签:
【中文标题】Datatable C# - 如何根据 ID 从一个表中获取详细记录及其关联的另一表中的子记录【英文标题】:Datatable C# - How to get detail record from one table and its associated sub record from another table based on ID 【发布时间】:2020-11-24 10:38:26 【问题描述】:我在数据表中有一条学生记录,如果我点击任何一条记录,它会执行两个操作。
获取学生的详细信息,它工作正常,如绿色圈出的图像中所述 使用 getJson。
我还想获取所选学生的所有发票及其付款。
注意:发票可以是一张或多张,付款也可以是一张或多张。
问题 - 用红色圈出
一个。我在数据表的第一行没有可用数据。
b.只从 Invoice 表中获取数据,而不从 payment 表中获取数据。
c。无法从表中选择记录。
寻找解决方案/解决方法
当用户从列表中选择任何学生时,它应该显示所有发票及其相应的付款,然后转到下一张发票及其相关付款。
例子:
学号:123
发票编号:12345、12347、123479
付款 ID:12 表示发票 ID 12345,14 表示 12345,17 表示 12347,依此类推。
JSON 代码:
var oTable1;
oTable1 = $('#invoiceNo').dataTable();
$('#invoiceNo').on('click', 'tr', function ()
var stu_id = $(this).find('td').eq(0).text();
var aid = parseInt(stu_id);
//alert(aid);
$.getJSON("/Transaction/showStu_Inv_Pay",
id: aid
,
function (data)
num_rows = data.length;
alert(num_rows);
var myTable = $('#inv_pay').DataTable();
myTable.clear().rows.add(myTable.data).draw();
$(data).each(function (index, item)
$('#inv_pay tbody').append(
'<tr><td>' + this.InvoiceID +
'</td><td>' + this.InvoiceIDate +
'</td><td>' + this.Invoice_Type_Name +
'</td></tr>'
)
);
);
);
Linq 查询
public IList<Transaction> GetInvByStuID()
try
DBAPPSEntities _db = new DBAPPSEntities();
this.Invoice_Type_Name = "Invoice";
IList<Transaction> List = (from q in _db.DEL_STU_INV
where q.STU_ID == this.Stu_ID
select new Transaction
InvoiceID = q.INVOICE_ID,
InvoiceIDate = q.Inv_Issue_Date,
InvoiceDDate = q.Inv_Due_Date,
Invoice_Month1 = q.Inv_Month_1,
Invoice_Month2 = q.Inv_Month_2,
Invoice_Month3 = q.Inv_Month_3,
Invoice_Note = q.Inv_Note,
Invoice_Adjustment = q.Adjust_Type,
Invoice_Type_Name = this.Invoice_Type_Name,
).ToList();
return List;
catch
return null;
注意:- linq 查询仅来自发票表,不确定如何与付款表结合,可以更改。
第 101 节
控制器 - 设置学生 ID
public JsonResult showStu_Inv_Pay(int id)
var model = new Transaction Stu_ID = id ; // student id set to model
var data = model.GetInvByStuID();
return Json(data, JsonRequestBehavior.AllowGet);
第 102 节
样本数据
第 103 节 - 更新 - 1
第 104 节 - 更新 - 2
【问题讨论】:
JSON 代码显示发票表有学生 ID,但您的 c# 表缺少学生 ID。 @jdweng 感谢您的调查。我在第 101 节下更新了关于控制器的问题。它工作正常。我在两件事上寻求帮助。一种。形成涉及多个表的 LINQ 查询(为了您的理解,我还将在今天晚些时候更新第 102 节下的采样数据)。 B. 根据匹配行的返回结果在 json 中创建数据表。 我已更新,如有遗漏请告诉我。 【参考方案1】:试试下面的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication164
class Program
static void Main(string[] args)
DBAPPSEntities _db = new DBAPPSEntities();
_db.DEL_STU_INV = new List<DEL_STU_INV>()
new DEL_STU_INV() INVOICE_ID = 59456, Inv_Issue_Date = new DateTime(20,6,1), Inv_Due_Date = new DateTime(20,6,15), STU_ID = 197, STU_Name = "New_Student1", Amount = 1000,
new DEL_STU_INV() INVOICE_ID = 59457, Inv_Issue_Date = new DateTime(20,6,1), Inv_Due_Date = new DateTime(20,6,10), STU_ID = 197, STU_Name = "New_Student2", Amount = 1000,
new DEL_STU_INV() INVOICE_ID = 59458, Inv_Issue_Date = new DateTime(20,6,1), Inv_Due_Date = new DateTime(20,6,10), STU_ID = 103, STU_Name = "New_Student3", Amount = 1000,
new DEL_STU_INV() INVOICE_ID = 59459, Inv_Issue_Date = new DateTime(20,6,1), Inv_Due_Date = new DateTime(20,6,10), STU_ID = 184, STU_Name = "New_Student4", Amount = 1000,
new DEL_STU_INV() INVOICE_ID = 59460, Inv_Issue_Date = new DateTime(20,6,1), Inv_Due_Date = new DateTime(20,6,10), STU_ID = 197, STU_Name = "New_Student5", Amount = 1000
;
_db.PAYMENT = new List<PAYMENT>()
new PAYMENT() PAYMENT_ID = 1, PDate = new DateTime(20,6,18), STU_ID = 197, INV_ID = 59456, Paid_Amount = 200, Balance = 800,
new PAYMENT() PAYMENT_ID = 2, PDate = new DateTime(20,6,17), STU_ID = 934, INV_ID = 59458, Paid_Amount = 500, Balance = 500,
new PAYMENT() PAYMENT_ID = 3, PDate = new DateTime(20,6,17), STU_ID = 197, INV_ID = 59456, Paid_Amount = 250, Balance = 550,
new PAYMENT() PAYMENT_ID = 4, PDate = new DateTime(20,6,17), STU_ID = 197, INV_ID = 59457, Paid_Amount = 1000, Balance = 0,
new PAYMENT() PAYMENT_ID = 5, PDate = new DateTime(20,6,17), STU_ID = 950, INV_ID = 59459, Paid_Amount = 1000, Balance = 0,
new PAYMENT() PAYMENT_ID = 6, PDate = new DateTime(20,6,17), STU_ID = 197, INV_ID = 59456, Paid_Amount = 500, Balance = 50,
new PAYMENT() PAYMENT_ID = 7, PDate = new DateTime(20,6,17), STU_ID = 196, INV_ID = 59458, Paid_Amount = 250, Balance = 250,
new PAYMENT() PAYMENT_ID = 8, PDate = new DateTime(20,6,17), STU_ID = 1060, INV_ID = 59458, Paid_Amount = 250, Balance = 0
;
int Stu_ID = 197;
var List = (from s in _db.DEL_STU_INV.Where(x => x.STU_ID == Stu_ID)
join p in _db.PAYMENT on s.INVOICE_ID equals p.INV_ID into ps
from z in ps.DefaultIfEmpty()
select new s = s, p = (z == null)? null : z
)
.GroupBy(x => x.s.INVOICE_ID)
.Select(x =>
new
invoice = new Result() type = "Invoice", id = x.Key, date = x.First().s.Inv_Issue_Date, amount = x.First().s.Amount ,
payments = x.Select(y => (y.p == null)? null : new Result() type = "Payment", id = y.p.PAYMENT_ID, date = y.p.PDate, amount = y.p.Paid_Amount )
).Select(y => (y.payments.Count() == 1) ? new List<Result>() y.invoice : (new List<Result>() y.invoice ).Concat(y.payments).ToList())
.ToList();
public class DBAPPSEntities
public List<DEL_STU_INV> DEL_STU_INV get;set;
public List<PAYMENT> PAYMENT get; set;
public class DEL_STU_INV
public int STU_ID get; set;
public string STU_Name get;set;
public int INVOICE_ID get; set;
public int Amount get; set;
public DateTime Inv_Issue_Date get; set;
public DateTime Inv_Due_Date get; set;
public string Inv_Month_1 get; set;
public string Inv_Month_2 get; set;
public string Inv_Month_3 get; set;
public string Inv_Note get; set;
public string Adjust_Type get; set;
public class PAYMENT
public int PAYMENT_ID get; set;
public DateTime PDate get; set;
public int STU_ID get; set;
public int INV_ID get; set;
public int Paid_Amount get; set;
public int Balance get; set;
public class Transaction
public int InvoiceID get;set;
public DateTime InvoiceIDate get;set;
public DateTime InvoiceDDate get;set;
public string Invoice_Month1 get;set;
public string Invoice_Month2 get;set;
public string Invoice_Month3 get;set;
public string Invoice_Note get;set;
public string Invoice_Adjustment get; set;
public string Invoice_Type_Name get; set;
public class Result
public string type get; set;
public int id get; set;
public DateTime date get; set;
public int amount get; set;
【讨论】:
嗨 @jdweng,我已经更新了第 103 节,如果有机会,请您查看一下。如果您还想知道我正在寻找的输出方式,我还可以更新该信息。 您正在使用实体,其中有一个 dbmx 映射文件,该文件将数据库中的表/列与 c# 代码中的类/属性进行映射。 PDate 未正确映射或拼写错误。 我确信我正在寻找的解决方案或解决方法在您的代码中,我必须根据我的要求进行调整。我认为 PDate(付款表字段)中没有错字。除此之外,我还更新了第 104 节下的问题。请您看一下。我正在寻找这种格式的数据表中的结果。您提供的解决方案没有返回语句,我正在将返回结果返回到控制器并从那里查看。 非常感谢您的时间、帮助和教学。 非常感谢。以上是关于Datatable C# - 如何根据 ID 从一个表中获取详细记录及其关联的另一表中的子记录的主要内容,如果未能解决你的问题,请参考以下文章