簡體   English   中英

如何使用Linq to Entities獲取具有最新日期的記錄組

[英]How to get group of records with latest date using Linq to Entities

我有一個包含以下字段的表:

ID | MarketID | CommodityID | CurrencyID | PriceValue | Year | Month

帶有以下數據:

1  | 100 | 30 | 15 | 3.465 | 2018 | 03
2  | 100 | 30 | 15 | 2.372 | 2018 | 04
3  | 100 | 32 | 15 | 1.431 | 2018 | 02
4  | 100 | 32 | 15 | 1.855 | 2018 | 03
5  | 100 | 32 | 15 | 2.065 | 2018 | 04
6  | 101 | 30 | 15 | 7.732 | 2018 | 03
7  | 101 | 30 | 15 | 8.978 | 2018 | 04
8  | 101 | 32 | 15 | 4.601 | 2018 | 02
9  | 101 | 32 | 18 | 0.138 | 2017 | 12
10 | 101 | 32 | 18 | 0.165 | 2018 | 03
11 | 101 | 32 | 18 | 0.202 | 2018 | 04

如您所見,(不幸的)日期被保存為“年”和“月”字段中的整數。

我想使用LINQ to Entities(EF6)從以上數據中獲取每個市場商品貨幣記錄的最新PriceValue。

因此, 預期結果應為:

2  | 100 | 30 | 15 | 2.372 | 2018 | 04
5  | 100 | 32 | 15 | 2.065 | 2018 | 04
7  | 101 | 30 | 15 | 8.978 | 2018 | 04
8  | 101 | 32 | 15 | 4.601 | 2018 | 02
11 | 101 | 32 | 18 | 0.202 | 2018 | 04

嘗試了以下查詢,但沒有一個能給我預期的結果:

var lastValues = (from a in Analysis
                     group a by a.ID into g
                     select g.OrderByDescending(t => ((t.Year* 100) + t.Month)));

和下面的內容對上一個內容有更多的了解,但是我松了PriceValue字段:

var lastValues = (from a in Analysis
                  group a by new {a.MarketID, a.CommodityID, a.CurrencyID } into g
                  select new
                  {
                     g.Key.MarketID,
                     g.Key.CommodityID,
                     g.Key.CurrencyID,
                     date = g.Max(t => ((t.Year* 100) + t.Month))
                  });

如上所述,有沒有辦法使單個LINQ查詢僅獲取具有最新日期的記錄?

嘗試以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication45
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("MARKETID", typeof(int));
            dt.Columns.Add("CommodityID", typeof(int));
            dt.Columns.Add("CurrencyID", typeof(int));
            dt.Columns.Add("PriceValue", typeof(decimal));
            dt.Columns.Add("Year", typeof(int));
            dt.Columns.Add("Month", typeof(int));
            //ID | MarketID | CommodityID | CurrencyID | PriceValue | Year | Month
            dt.Rows.Add(new object[] {1  , 100 , 30 , 15 , 3.465 , 2018 , 03});
            dt.Rows.Add(new object[] {2  , 100 , 30 , 15 , 2.372 , 2018 , 04});
            dt.Rows.Add(new object[] {3  , 100 , 32 , 15 , 1.431 , 2018 , 02});
            dt.Rows.Add(new object[] {4  , 100 , 32 , 15 , 1.855 , 2018 , 03});
            dt.Rows.Add(new object[] {5  , 100 , 32 , 15 , 2.065 , 2018 , 04});
            dt.Rows.Add(new object[] {6  , 101 , 30 , 15 , 7.732 , 2018 , 03});
            dt.Rows.Add(new object[] {7  , 101 , 30 , 15 , 8.978 , 2018 , 04});
            dt.Rows.Add(new object[] {8  , 101 , 32 , 15 , 4.601 , 2018 , 02});
            dt.Rows.Add(new object[] {9  , 101 , 32 , 18 , 0.138 , 2017 , 12});
            dt.Rows.Add(new object[] {10 , 101 , 32 , 18 , 0.165 , 2018 , 03});
            dt.Rows.Add(new object[] { 11, 101, 32, 18, 0.202, 2018, 04 });

            List<DataRow> results = dt.AsEnumerable()
                .OrderByDescending(x => new DateTime(x.Field<int>("Year"), x.Field<int>("Month"), 1))
                .GroupBy(x => new { market = x.Field<int>("MarketID"), commodity = x.Field<int>("CommodityID"), currency = x.Field<int>("CurrencyID") })
                .Select(x => x.First())
                .ToList();


        }

    }
}

我終於找到了在結果中也包含PriceValue的方法。

從我發布的最后一個查詢開始,我只需添加

g.OrderByDescending(t => ((t.Year* 100) + t.Month)).FirstOrDefault().PriceValue

因此它將是:

var lastValues = (from a in Analysis
                  group a by new {a.MarketID, a.CommodityID, a.CurrencyID } into g
                  select new
                  {
                     g.Key.MarketID,
                     g.Key.CommodityID,
                     g.Key.CurrencyID,
                     date = g.Max(t => ((t.Year* 100) + t.Month)),
                     g.OrderByDescending(t => ((t.Year* 100) + t.Month)).FirstOrDefault().PriceValue
                  });

我更喜歡這種解決方案,因為它基於我目前開發的查詢。

試試這個Linq查詢

     var lastValues = (from ak in Analysis
                          group ak by new { ak.MarketID, ak.CommodityID, ak.CurrencyID } into g
                          select new
                          {
                              g.Key.MarketID,
                              g.Key.CommodityID,
                              g.Key.CurrencyID,
                              pricevalue = g.OrderByDescending(c=>c.Year).ThenByDescending(c=>c.Month).FirstOrDefault().PriceValue,
                              year = g.OrderByDescending(c => c.Year).ThenByDescending(c => c.Month).FirstOrDefault().Year,
                              month = g.OrderByDescending(c => c.Year).ThenByDescending(c => c.Month).FirstOrDefault().Month
                          });

使用Orderby和ThenBy

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM