【问题标题】:How to Take Top 10 Distinct Rows order by Date如何按日期获取前 10 个不同行的顺序
【发布时间】:2013-12-05 16:50:35
【问题描述】:

我有一个数据表有这样的记录...

JID                      Date                     RecentChatString
----------------------------------------------------------------------- 
abc@gmail.com         11/Nov/2013 11:53:00        Hi 
abc@gmail.com         11/Nov/2013 11:53:10        Hello 
abc@gmail.com         11/Nov/2013 11:54:00        Good Morning 
def@gmail.com         11/Nov/2013 12:03:00        Ok 
abc@gmail.com         11/Nov/2013 12:05:10        Please reply 
def@gmail.com         11/Nov/2013 12:15:00        Good after noon 
def@gmail.com         11/Nov/2013 12:15:50        Ok bye

我想获取按日期排序的 Top 10 Distinct Records,仅表示每个 JID 的最近聊天..

JID                      Date                     RecentChatString
-----------------------------------------------------------------------
abc@gmail.com         11/Nov/2013 12:05:10        Please reply
def@gmail.com         11/Nov/2013 12:15:50        Ok bye

现在我有这样的代码。我可以使用此代码获取按日期排序的前 10 条记录。 但是,它包含重复的JID's。请帮我。 (recent_index 是一个数据表)

DataRow recent_dr = recent_index.NewRow();
recent_dr["JID"] = RosterId;
recent_dr["Date"] = DateTime.Now;
recent_dr["RecentChatString"] = _chatline;
recent_index.Rows.Add(recent_dr);

DataTable dtt = new DataTable("RecentChats");
dtt.Columns.Add("JID", Type.GetType("System.String"));
dtt.Columns.Add("Date", Type.GetType("System.DateTime"));
dtt.Columns.Add("RecentChatString", Type.GetType("System.String"));

IEnumerable<DataRow> recentTen = recent_index.AsEnumerable().OrderByDescending(x=>x["Date"]).Take(10);
recentTen.CopyToDataTable(dtt, LoadOption.OverwriteChanges);

dtt.WriteXml(s + "\\FPhoneData\\chats\\index.xml");

【问题讨论】:

  • 什么数据库引擎?
  • 你试过.Distinct吗?
  • @MichaelPerrenoud XML...

标签: c# linq datatable dataset


【解决方案1】:

蒂姆的解决方案是正确的。这只是声明式查询的替代解决方案:

var latestMessages =
    from r in recent_index.AsEnumerable()
    group r by r.Field<string>("JID") into g
    let latest = g.OrderByDescending(r => r.Field<DateTime>("Date")).First()
    orderby latest.Field<DateTime>("Date")
    select latest;

var recent_dr = latestMessages.Take(10).CopyToDataTable();

【讨论】:

    【解决方案2】:

    在您的集合上使用 .Distinct 使用自定义类进行不同检查

    private class DistinctChecker: IEqualityComparer<YourRowType>
    {
    }
    

    【讨论】:

      【解决方案3】:
      DataTable recentTen = recent_index.AsEnumerable()
          .OrderByDescending(r => r.Field<DateTime>("Date"))
          .GroupBy(r => r.Field<string>("JID"))
          .Take(10)
          .Select(g => g.First())
          .CopyToDataTable();
      

      【讨论】:

      • 然后 LINQ 主机再次通过。有你的力量很强大。
      • 这不是随机抽取 10 个 JID 并订购吗?不需要在 Take() 之前下单吗?
      • @JoachimIsaksson:好点,编辑了我的答案。第一种方法效率更高,但会产生任意行,其中仅考虑 JID 组的顺序。
      【解决方案4】:

      尝试以下操作:

      DataView view = new DataView(recent_index);
      DataTable distinctValues = view.ToTable(true, "JID", "Date" ...);
      

      【讨论】:

        猜你喜欢
        • 2021-08-02
        • 1970-01-01
        • 2020-05-06
        • 1970-01-01
        • 1970-01-01
        • 2022-12-04
        • 1970-01-01
        • 1970-01-01
        • 2017-04-15
        相关资源
        最近更新 更多