Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I have following records
entry_date, employee_id, employee_name, details
10/11/2018      1        KING           PRODUCTION
10/11/2018      2        PRINCE         TYPING
11/11/2018      2        PRINCE         HR
11/11/2018      3        KANNAN         SPORTS
11/11/2018      7        RAVI           ADMIN
12/11/2018      4        KUMAR          ACCOUNTS
13/11/2018      5        RAMAN          PRODUCTION
13/11/2018      7        RAVI           HR

From the above records in datatable I wish to select the first record /per day
like the following
10/11/2018      1        KING           PRODUCTION
11/11/2018      2        PRINCE         HR
12/11/2018      4        KUMAR          ACCOUNTS
13/11/2018      5        RAMAN          PRODUCTION

Is it possible?

Thanks

What I have tried:

Select rows of datatable by distinct Date & minimum value of employee_id
Posted
Updated 24-Nov-18 5:55am
Comments
OriginalGriff 24-Nov-18 4:17am    
So show us the code you have tried, and explain what it does that you didn't expect, or doesn't do that you did.

Use the "Improve question" widget to edit your question and provide better information.

1 solution

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

            // Add columns
            DataTable dt = new DataTable();
            dt.Columns.Add("entry_date");
            dt.Columns.Add("employee_id");
            dt.Columns.Add("employee_name");
            dt.Columns.Add("details");

            // Add test data
            dt.Rows.Add(new string[] { "10/11/2018", "2", "PRINCE", "TYPING" });
            dt.Rows.Add(new string[] { "10/11/2018", "1", "KING", "PRODUCTION" });
            dt.Rows.Add(new string[] { "11/11/2018", "7", "RAVI", "ADMIN" });
            dt.Rows.Add(new string[] { "11/11/2018", "3", "KANNAN", "SPORTS" });
            dt.Rows.Add(new string[] { "11/11/2018", "2", "PRINCE", "HR" });
            dt.Rows.Add(new string[] { "12/11/2018", "4", "KUMAR", "ACCOUNTS" });
            dt.Rows.Add(new string[] { "13/11/2018", "7", "RAVI", "HR" });
            dt.Rows.Add(new string[] { "13/11/2018", "5", "RAMAN", "PRODUCTION" });

            IEnumerable<DataRow> result = dt.AsEnumerable()
                .OrderBy(r => r.Field<string>("entry_date"))
                .ThenBy(r => r.Field<string>("employee_id"))
                .GroupBy(r => r.Field<string>("entry_date")).Select(g => g.First());

            foreach (var dtrow in result)
            {
                Debug.Print(string.Join("\t", dtrow.ItemArray));
            }
 
Share this answer
 
v2

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