none
Getting count value from one table, and put this value into another RRS feed

  • Question

  • I have a table called dataTable_latest which contains a column called LearnRefNumber which is populated with about 150 rows. 

    What I need to is to be able to take the distinct count of the LearnRefNumber column from dataTable_latest (and for another thing i'm doing, a total count), and put the value into an empty data table I have called ULINCounts, in a column called Count. This will just be one row that's it's populating in ULINCounts. 

    Friday, June 21, 2019 3:55 PM

Answers

All replies

  • Have you tried something like this

    https://stackoverflow.com/questions/18242830/how-do-i-select-the-distinct-row-count-of-a-column-in-a-data-table

    int relationshipCount = table
        .AsEnumerable()
        .Select(r => r.Field<string>("RelationshipTypeDescription"))
        .Distinct()
        .Count();


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by voot1989 Monday, June 24, 2019 8:19 AM
    Friday, June 21, 2019 4:53 PM
    Moderator
  • Hi voot1989,

    Thank you for posting here.

    Based on your description, you want to get the distinct count of the LearnRefNumber column and put the count in another datatable.

    You could try the following code.

     DataTable dataTable_latest = new DataTable();
                dataTable_latest.Columns.Add("Id", typeof(int));
                dataTable_latest.Columns.Add("LearnRefNumber", typeof(int));
                dataTable_latest.Rows.Add(1, 15);
                dataTable_latest.Rows.Add(2, 16);
                dataTable_latest.Rows.Add(3, 37);
                dataTable_latest.Rows.Add(4, 45);
                dataTable_latest.Rows.Add(5, 16);
                dataTable_latest.Rows.Add(6, 18);
                List<int> list = new List<int>();
                foreach (DataRow item in dataTable_latest.Rows)
                {
                    list.Add(Convert.ToInt32(item["LearnRefNumber"]));
                }
                int count = list.Distinct().Count();
                DataTable ULINCounts = new DataTable();
                ULINCounts.Columns.Add("Count", typeof(int));
                ULINCounts.Rows.Add(count);

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 24, 2019 2:06 AM
    Moderator
  • Thanks for your reply, it's greatly appreciated. 

    Can certainly see your angle here, but a fully populated table of "datatable_latest" already exists with a LearnRefNumber column. Therefore, I won't need a new datatable as detailed from the first part of your suggestion, won't I?

    Monday, June 24, 2019 7:53 AM
  • Hi 

    Thanks for the feedback.

    I am not sure what you mean. I want to know if your question has been solved, If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue. If not, please feel free to let us know.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 24, 2019 7:57 AM
    Moderator
  • Hi Jack. 

    No, not solved quite yet. It's just the the table you advise me to build as a new table -datatable_latest, already exists, so doesn't need to be built again.

    Monday, June 24, 2019 8:10 AM