Click here to Skip to main content
15,912,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a Datatable having records like this...

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


I want to get Top 10 Distinct Records sorted by Date and, Means only the recent chat of each 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


Now I have a code like this... I can get the top 10 records sorted by date using this code...
But, It contains duplicate JID's.... Please help me .... (recent_index is a DataTable)

C#
 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");
Posted
Updated 20-Nov-13 22:35pm
v2

Include Distinct() at the end and check.
C#
IEnumerable<datarow> recentTen = GetTable().AsEnumerable().OrderByDescending(x => x["Date"]).Take(10).Distinct();
 
Share this answer
 
v2
Here is the solution for your query


here i am creating a DataTable

DataTable dt = new DataTable("mytable");
      DataColumn dc1 = new DataColumn("ID", typeof(int));
      dc1.AutoIncrement = true;
      dc1.AutoIncrementSeed = 1;
      dc1.AutoIncrementStep = 1;
      dt.Columns.Add(dc1);
      dt.Columns.Add(new DataColumn("name", typeof(string)));
      dt.Columns.Add(new DataColumn("age", typeof(string)));
      dt.Columns.Add(new DataColumn("date", typeof(DateTime)));

      for (int i = 0; i < 10; i++)
      {
          DataRow dr = dt.NewRow();
          dr["name"] = "dasda " + i.ToString();
          dr["age"] = (i * i).ToString();

          switch (i)
          {
              case 0: dr["date"] = DateTime.Now.AddDays(i);
                  break;
              case 1: dr["date"] = DateTime.Now.AddDays(2);
                  break;
              case 2: dr["date"] = DateTime.Now.AddDays(i * i);
                  break;
              case 3: dr["date"] = DateTime.Now.AddDays(i * i);
                  break;
              case 4: dr["date"] = DateTime.Now.AddDays(2);
                  break;
              case 5: dr["date"] = DateTime.Now.AddDays(i * i);
                  break;
              case 6: dr["date"] = DateTime.Now.AddDays(2);
                  break;
              case 7: dr["date"] = DateTime.Now.AddDays(i * i);
                  break;
              case 8: dr["date"] = DateTime.Now.AddDays(i * i);
                  break;
              case 9: dr["date"] = DateTime.Now.AddDays(2);
                  break;
              default:
                  break;
          }
          dt.Rows.Add(dr);

      }


now i am firing the LINQ query to fetch the records as you want..

XML
var xyz = (from m in dt.Select()
                  let _name = m.Field<string>("name")
                  let _date = m.Field<DateTime>("date")
                  let _age = m.Field<string>("age")
                  let _id = m.Field<int>("id")
                  orderby _date descending
                  group m by _date into g
                  select new
                  {
                      _name = g.First()["name"].ToString(),
                      _id = Convert.ToInt32(g.First()["ID"]),
                      _age = g.First()["age"].ToString(),                     
                      _date = (DateTime)g.First()["date"]                     
                  }).ToList();


this query will give the distinct record on the behalf of date.for fetching last ten record i used order by descending.
you want top 10 record then simply call TAKE() you wiil get the result.

var res= xyz.Take(10);


you will get the top 10 records.
 
Share this answer
 
v2
Comments
Er Daljeet Singh 22-Nov-13 1:00am    
i made some changes in query now execute and test it.
quite similar to previous one but working fine...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900