简体   繁体   English

如何使用 LINQ 获取最后一条记录 - SQLite 中的实体框架

[英]How to Get Last Record Using LINQ - Entity Framework in SQLite

I need to translate the following SQLite query to LINQ in C#我需要将以下 SQLite 查询转换为 C# 中的 LINQ

SELECT sup.SupplierName, sup.SupplierID, pr.Price, max(pr.AddedDate)
FROM Suppliers sup
LEFT JOIN ItemsPrices pr
USING(SupplierID)
WHERE pr.ItemID = '22'
GROUP BY sup.SupplierName

I've searched about all web site and tried the following LINQ query and it does group like what I want but doesn't select latest date.我已经搜索了所有 web 网站并尝试了以下 LINQ 查询,它确实按照我想要的方式分组,但不是 select 最新日期。 I'm newbie in LINQ please help me我是 LINQ 的新手,请帮助我

    internal List<LatestPriceDbo> GetLatestPrice(int _itemid)
    {
        using (dbContext context = new dbContext())
        {

             var groupedPrice = from a in context.ItemsPrices
                             where a.ItemId == _itemid
                             orderby a.Id descending
                             group a by new { a.ItemId, a.SupplierId } into g
                             select new ItemsPrice
                             {
                                SupplierId = g.Key.SupplierId,
                                ItemId = g.Key.ItemId,
                                Price = g.FirstOrDefault().Price,
                                AddedDate = g.Max(s => s.AddedDate)
                             };

            var result = (from c in context.Suppliers
                        from k in groupedPrice
                        where k.ItemId == _itemid && c.SupplierId == k.SupplierId
                        select new LatestPriceDbo
                        {
                             supid = c.SupplierId,
                             supname = c.SupplierName,
                             price = k.Price,
                             addeddate = k.AddedDate
                        }).ToList();


          return result;
       }
    }


internal class LatestPriceDbo
{
    public int supid { get; set; }
    public string supname { get; set; }
    public decimal price { get; set; }
    public string addeddate { get; set; }
}

I am using Database-First.我正在使用数据库优先。

You should be able to use a LINQ Join I have mocked up something which might point you in the correct direction:您应该能够使用 LINQ Join 我已经模拟了一些可能为您指明正确方向的东西:

Notes笔记

  1. use the join first to get the set you looking for首先使用加入来获取您正在寻找的集合
  2. you can then do a nested select for the max based on supplierId.然后,您可以根据supplierId 对最大值进行嵌套选择。
 from a in context.ItemsPrices 
      join s in context.Suppliers on a.SupplierId equals s.supplierId 
                 where a.ItemId == _itemid
                 orderby a.Id descending
                 select new ItemsPrice
                 {
                    SupplierName = s.SupplierName
                    SupplierId = a.SupplierId,
                    ItemId = a.ItemId,
                    Price = a.FirstOrDefault().Price,
                    AddedDate = context.ItemsPrices.Where(x => x.SupplierId == a.SupplierId).Max(s => s.AddedDate)
                 };

I would like to send this as a comment but i don't have enough reputation.我想将此作为评论发送,但我没有足够的声誉。

So if you do the query you want and after that you order it by date descending and if you scale it with top 1 i think it might be work.因此,如果您执行所需的查询,然后按日期降序对其进行排序,并且如果您使用前 1 对其进行缩放,我认为它可能会起作用。

I solved the problem owing to Kevin's suggestion.由于凯文的建议,我解决了这个问题。 I did need to be more search on web to improve the code block of Kevin's comment and I did.我确实需要在 web 上进行更多搜索,以改进 Kevin 评论的代码块,我做到了。

    internal List<LatestPriceDbo> GetLatestPrice(int _itemid)
    {
        using (dbContext context = new dbContext())
        {
            var result = (from a in context.ItemsPrices
                          join s in context.Suppliers on a.SupplierId equals s.SupplierId
                          where a.ItemId == _itemid
                          orderby a.Id descending
                          group new { a, s } by new { a.SupplierId, a.ItemId } into grb
                          select new LatestPriceDbo
                          {
                              supname = grb.FirstOrDefault().s.SupplierName,
                              supid = grb.Key.SupplierId,
                              itemid = grb.Key.ItemId,
                              price = context.ItemsPrices
                                          .Where(x => x.ItemId == grb.FirstOrDefault().a.ItemId)
                                          .OrderByDescending(z => z.Id).Select(z => z.Price)
                                          .FirstOrDefault(),
                              addeddate = context.ItemsPrices
                                          .Where(x => x.SupplierId == grb.FirstOrDefault().a.SupplierId)
                                          .Max(s => s.AddedDate)
                          }).ToList();

            return result;
        }
    }

    internal class LatestPriceDbo
    {
        public int itemid { get; set; }
        public int supid { get; set; }
        public string supname { get; set; }
        public decimal price { get; set; }
        public string addeddate { get; set; }
        public int recordid { get; set; }
    }

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

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