简体   繁体   English

获取数据库中表的最后一条记录?

[英]get last record of a table in database?

i am developing a project and after the user insert the data of the customer he suppose to print the ID and the name of the Customer, this is the code of the ID我正在开发一个项目,在用户插入客户的数据后,他想打印客户的 ID 和名称,这是 ID 的代码

int getPatnID()
    {
        int ID = (from i in Db.Patients
                  select i.patn_ID).Max();
        return ID;
    }

i have write the code to get the name but i cant finish it我已经编写了代码来获取名称,但我无法完成它

string getPatnName()
    {
        string name = (from n in Db.Patients
                       select n.patn_name).

        return name;
    }

How should i write to complete it??我该怎么写才能完成它?

This should always get the last inserted record at time of execution (assuming patn_id is auto-incrementing)这应该总是在执行时获得最后插入的记录(假设 patn_id 是自动递增的)

string GetPatnName() 
{
    string name = (from n in Db.Patients 
                   orderby n.patn_ID descending 
                   select n.patn_name).FirstOrDefault();
    return name;
}
string getPatnName() {
    string name = Db.Patients.Last(p => p.patn_ID).patn_name;
    return name;
}

I suggest you spend some more time studying LINQ - it is a very rich and powerful library: http://msdn.microsoft.com/en-us/library/bb397926.aspx我建议你多花点时间研究 LINQ - 它是一个非常丰富和强大的库: http://msdn.microsoft.com/en-us/library/bb397926.aspx

Change getPatnName to take an id:更改getPatnName以获取 id:

string getPatnName(int id)
{
    string name = (from n in Db.Patients
                   where n.patn_ID == id
                   select n.patn_name).SingleOrDefault();

    return name;
}

and give it the result of getPatnID()并给它getPatnID()的结果

If you are trying to get the name of the last patient you just entered, and you have the ID from the first query, you just need a where clause in your LINQ.如果您尝试获取刚刚输入的最后一位患者的姓名,并且您有第一个查询的 ID,则只需要 LINQ 中的 where 子句。

string name = (from n in Db.Patients
       where n.patn_ID == ID
       select n.patn_name);

Getting the id the way you do is wrong.以您的方式获取 id 是错误的。 Not sure what you're using, but both linq-2-sql and entity framework fill in the entity Id when saving changes:不确定您使用的是什么,但 linq-2-sql 和实体框架在保存更改时都会填写实体 ID:

    Patient patient = new Patient();
    Console.WriteLine(patient.Id); // will NOT be filled in
    Db.Patients.Add(patient);
    Db.SubmitChanges(); // SaveChanges in EF
    Console.WriteLine(patient.Id); // will be filled in

Why not just create a patient class an return an instance as shown below为什么不只创建一个患者 class 并返回一个实例,如下所示

public class Patient
{
    public int PatientID { get; set; }
    public string Name { get; set; }
}


static Patient getDetails()
    {
        using (DataClasses1DataContext db = new DataClasses1DataContext())
        {
            var c = (from x in db.Patients.OrderByDescending(u => u.PatientID)
                     select new Patient()
                     {
                         PatientID = x.PatientID,
                         Name = x.Name
                     }).FirstOrDefault();
            return c;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM