How to return count with list of rows ?

ahmed salah 3,216 Reputation points
2023-02-06T22:08:10.3+00:00

I work on c# I face issue I can't return count of rows with list of rows returned .
so I need to return list of rows and this id done

remaining to return count with it

so How to do it please ?

Employee Model
public class EmpModel
{
    public int EmployeeId { get; set; }
    public string EmployeeName { get; set; }
    public int EmployeeStatus { get; set; }
}

public List<EmpModel> GetAllEmployees()
{
    connection();
    List<EmpModel> EmpList = new List<EmpModel>();

    SqlCommand com = new SqlCommand("GetEmployees", con);
    com.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter da = new SqlDataAdapter(com);
    DataTable dt = new DataTable();

    con.Open();
    da.Fill(dt);
    con.Close();   
    foreach (DataRow dr in dt.Rows)
    {

        EmpList.Add(
            new EmpModel
            {
                EmployeeId = Convert.ToInt32(dr["EmployeeId"]),
                EmployeeName = Convert.ToString(dr["EmployeeName"]),
                EmployeeStatus = Convert.ToInt32(dr["EmployeeStatus"])
            }
            );
    }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,919 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,421 Reputation points
    2023-02-06T23:32:57.7733333+00:00

    You can return both the list and the count as follows which is a proof of concept which deconstructs at the caller end.

    public class EmpModel
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public int EmployeeStatus { get; set; }
    }
    
    public class Operations
    {
        public static (List<EmpModel> list, int count)  GetAllEmployees()
        {
            connection();
            List<EmpModel> EmpList = new List<EmpModel>();
    
            SqlCommand com = new SqlCommand("GetEmployees", con);
            com.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataTable dt = new DataTable();
    
            con.Open();
            da.Fill(dt);
            con.Close();
            foreach (DataRow dr in dt.Rows)
            {
    
                EmpList.Add(
                    new EmpModel
                    {
                        EmployeeId = Convert.ToInt32(dr["EmployeeId"]),
                        EmployeeName = Convert.ToString(dr["EmployeeName"]),
                        EmployeeStatus = Convert.ToInt32(dr["EmployeeStatus"])
                    }
                );
            }
    
            return (EmpList, EmpList.Count);
        }
    }
    
    public class CallingClass
    {
        public static void Run()
        {
            var (list, count) = Operations.GetAllEmployees();
        }
    }	
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Reza Aghaei 4,946 Reputation points MVP
    2023-02-06T22:15:54.49+00:00

    With a List<T> you don't need the count, the list already has a Count property. Use that one.

    var employees = GetAllEmployees();
    var count = employees.Count;
    

    Using a custom model

    If for a reason (that I cannot imagine 😄) you want to return the count in the output, you can create a model like this:

    using System.Collections.Generic;
    public class MyModel<T>
    {
        public MyModel(List<T> list) { this.List = list; }
        public List<T> List { get; set; }
        public int Count { get { return List.Count; } }
    }
    

    And in your method return new MyModel(EmpList);.

    Then you can use it like this:

    var model = GetAllEmployees();
    var employees = model.List;
    var count = model.Count;
    

    Using Tuple

    Another option could be returning a tuple; so you can modify your method like this:

    public (List<Employee>, int) GetAllEmployees()
    {
        ... 
        ...
        var count = list.Count;
        return (EmpList, EmpList.Count);
    }
    

    Then you can use it like this:

    var result = GetAllEmployees();
    var employees = result.Item1;
    var count = result.Item2;
    

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.