C# 在linq扩展方法中从表中检索最后一个值并与DataGridView绑定

C# 在linq扩展方法中从表中检索最后一个值并与DataGridView绑定,c#,winforms,entity-framework,linq,C#,Winforms,Entity Framework,Linq,我正在使用windows窗体应用程序。我想从数据库中检索最后一条记录并将其与datagridview绑定,我可以从中获取所有值 var query2 = _context.Products.Join(_context.ProductInfos, c => c.Id, a => a.ProductId, (a, c) => new { Id = a.Id, ItemName = a.ItemName,

我正在使用windows窗体应用程序。我想从数据库中检索最后一条记录并将其与datagridview绑定,我可以从中获取所有值

var query2 = _context.Products.Join(_context.ProductInfos, c => c.Id, a => a.ProductId, (a, c) => new
        {
            Id = a.Id,
            ItemName = a.ItemName,
            CategoryName = a.Category.CategoryName,
            UnitName = a.Unit.UnitName,
            UnitValue = a.UnitSize,
            Quantity = a.Quantity,
            CostPrice = c.PurchasePrice,
            SalePrice = c.SalePrice,
            EntryDate = c.EntryDate,
            ExpireDate = c.ExpireDate
        }).toList();

        StockListGrid.DataSource = query2;
但我只想要最后插入的值,我使用

var query2 = _context.Products.Join(_context.ProductInfos, c => c.Id, a => a.ProductId, (a, c) => new
        {
            Id = a.Id,
            ItemName = a.ItemName,
            CategoryName = a.Category.CategoryName,
            UnitName = a.Unit.UnitName,
            UnitValue = a.UnitSize,
            Quantity = a.Quantity,
            CostPrice = c.PurchasePrice,
            SalePrice = c.SalePrice,
            EntryDate = c.EntryDate,
            ExpireDate = c.ExpireDate
        }).ToList().LastOrDefault();

        StockListGrid.DataSource = query2;

但这次我没有得到任何值。请告诉我如何检索上次插入的值?

尝试使用OrderByDescending

var query2 = _context.Products.Join(_context.ProductInfos, c => c.Id, a => a.ProductId, (a, c) => new
    {
        Id = a.Id,
        ItemName = a.ItemName,
        CategoryName = a.Category.CategoryName,
        UnitName = a.Unit.UnitName,
        UnitValue = a.UnitSize,
        Quantity = a.Quantity,
        CostPrice = c.PurchasePrice,
        SalePrice = c.SalePrice,
        EntryDate = c.EntryDate,
        ExpireDate = c.ExpireDate
    }).OrderByDescending(x => x.Id).First();
还是马克斯

var query2 = _context.Products.Join(_context.ProductInfos, c => c.Id, a => a.ProductId, (a, c) => new
{
    Id = a.Id,
    ItemName = a.ItemName,
    CategoryName = a.Category.CategoryName,
    UnitName = a.Unit.UnitName,
    UnitValue = a.UnitSize,
    Quantity = a.Quantity,
    CostPrice = c.PurchasePrice,
    SalePrice = c.SalePrice,
    EntryDate = c.EntryDate,
    ExpireDate = c.ExpireDate
}).Max(x => x.Id);

第一个查询的类型是列表,所有元素都是相同的匿名类型
Anonymous1
。后面查询的类型是一个类为
Anonymous1
的对象。您的
StockListGrid.DataSource
期望得到什么?列表还是单个对象

ToList
将所有元素从数据库管理系统(DBMS)传输到本地内存,然后决定只需要最后一个元素

我看到两个问题

  • 如果只需要最后一个元素,为什么要传输所有元素
  • 是否定义了joinresult的最后一个元素?它是Id最高的一个吗?或者可能是最后一个字母
    ItemName
    ?它是卖价最高的那一个吗
不幸的是,实体框架不支持LastOrDefault。看

这个技巧将按你上次考虑的属性按升序排序,然后采用<代码> FrestRealSuth。请记住(取决于排序属性),可能有多个项目具有相同的排序值,因此需要进行第二次排序

var result = dbContext.Products.Join(dbContext.ProductInfos, ...)
    .OrderByDescending(joinResult => joinResult.SalePrice) // depending on your definition of Last

    .ThenByDescending(joinResult => joinResult.Id)         // in case there are two with same price
    .FirstOrDefault();
这更有效,因为只有一个元素从DBMS传输到本地内存

请注意,结果只有一个元素,而不是列表。如果要将仅包含最后一个元素的
列表
分配给
数据源

.ThenByDescending(joinResult => joinResult.Id)  
.Take(1)
.ToList();

同样,只传输一个匿名对象

如果
Id
是一个标识列,您可以按Id降序排序,然后取前1。您的意思是什么,但这次我没有得到任何值
.LastOrDefault()
将返回一个对象,而不是对象的集合。尝试
query.OrderByDescending(c=>c.Id).Take(1.ToList()