简体   繁体   English

从linq Extension方法中的表中检索最后一个值并与DataGridView绑定

[英]retrieve last value from table in linq Extention method and bind with DataGridView

I am working on windows Form App. 我正在使用Windows Form App。 I want to retrieve last record from database and bind it with datagridview, I can get all value from this 我想从数据库中检索最后一条记录并将其与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;

but i only want the last inserted value, i use 但我只想要最后插入的值,我用

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;

but this time i get no value.Please tell me how can i retrieve last inserted value? 但是这次我没有任何价值。请告诉我如何获取上次插入的价值?

Try using OrderByDescending 尝试使用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();

Or Max 或最大

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);

The type of your first query is a List<...>, all elements are of the same anonymous type Anonymous1 . 您的第一个查询的类型是List <...>,所有元素都具有相同的匿名类型Anonymous1 The type of your later query is one object of class Anonymous1 . 稍后查询的类型是Anonymous1类的一个对象。 What does your StockListGrid.DataSource expect? 您的StockListGrid.DataSource什么期望? A list or one single object? 一个列表还是一个对象?

ToList transports all elements from your Database management system (DBMS) to your local memory, after which you decide that you only want the last element ToList将所有元素从数据库管理系统(DBMS)传输到本地内存,然后您决定只需要最后一个元素

I see two problems 我看到两个问题

  • Why transport all elements if you only want the last one? 如果只想要最后一个元素,为什么还要运输所有元素?
  • Is the last element of your joinresult defined? 是否定义了joinresult的最后一个元素? Is it the one with the highest Id ? Id最高的那个吗? Or maybe the one with alphabetically last ItemName ? 或者,也许是按字母顺序最后一个ItemName那个? Is it the one with the highest SalePrice ? 这是不是一个最高SalePrice

Unfortunately Entity Framework does not support LastOrDefault. 不幸的是,实体框架不支持LastOrDefault。 See Supported and Unsupported LINQ methods (linq to entities) 请参阅受支持和不受支持的LINQ方法(对实体的linq)

The trick around this would be sorting in ascending order by the property you consider last and then take the FirstOrDefault . 解决这个问题的技巧是按照您最后考虑的属性以升序排序,然后采用FirstOrDefault Keep in mind that (depending on your sort property) there might be several items with the same sort value, so a second sort is needed 请记住,(取决于您的排序属性)可能会有多个具有相同排序值的项目,因此需要第二种排序

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();

This is much more efficient, because only one element is transported from your DBMS to your local memory. 这是非常有效的,因为只有一个元素从DBMS传输到本地内存。

Note that the result is only one element, not a List. 请注意,结果只是一个元素,而不是列表。 If you want assign a List with only this last element to your DataSource 如果您想将仅包含最后一个元素的List分配给您的DataSource

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

Again, only one anonymous object is transported 同样,仅传输一个匿名对象

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

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