简体   繁体   中英

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

I am working on windows Form App. I want to retrieve last record from database and bind it with datagridview, I can get all value from this

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

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 . The type of your later query is one object of class Anonymous1 . What does your StockListGrid.DataSource expect? 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

I see two problems

  • Why transport all elements if you only want the last one?
  • Is the last element of your joinresult defined? Is it the one with the highest Id ? Or maybe the one with alphabetically last ItemName ? Is it the one with the highest SalePrice ?

Unfortunately Entity Framework does not support LastOrDefault. See Supported and Unsupported LINQ methods (linq to entities)

The trick around this would be sorting in ascending order by the property you consider last and then take the 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.

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

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

Again, only one anonymous object is transported

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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