Aravind
Aravind

Reputation: 27

how to convert object's properties as list elements

I am trying to get all data from DB and display it in a table using ajax and stored procedure.

public List<string> ShowDetailsFromDB()
{
    using (adoHelper = new AdoHelper(connectionString))
    {
        List<string> users = new List<string>();  
        string procedureName = "GetDetails";
        SqlDataReader dataReader = adoHelper.ExecuteDataReaderByProcedure(procedureName);
        while (dataReader.Read())
        {
            User user = new User();
            user.userId = dataReader[1] as string;
            user.password = dataReader[2] as string;
            user.userName = dataReader[3] as string;
            user.address = dataReader[4] as string;
            user.email = dataReader[5] as string;
            user.phone = dataReader[6] as string;

            //here I want to assign each object property as list element

        }
        return users;
    }
}

Upvotes: 1

Views: 182

Answers (2)

John Alexiou
John Alexiou

Reputation: 29274

Below are two ways to generate a list of strings from the properties of a User instance.

internal class User
{

    public string userId { get; set; }
    public string password { get; set; }
    public string userName { get; set; }
    public string address { get; set; }
    public string email { get; set; }
    public string phone { get; set; }


    public string[] GetProperties()
    {
        return new string[]
        {
            userId,
            password,
            userName,
            address,
            email,
            phone
        };
    }

    static PropertyInfo[] properties = typeof(User).GetProperties();

    public string[] GetPropertiesAuto()
    {
        return properties.Select((prop) => prop.GetValue(this) as string).ToArray();
    }
}

The above can be used in your code quite simply, although you have to return a list of string array to get all the properties for all the users.

    static public List<string[]> ShowDetailsFromDB()
    {
        using (var adoHelper = new AdoHelper(connectionString))
        {
            List<string[]> users = new List<string[]>();
            string procedureName = "GetDetails";
            SqlDataReader dataReader = adoHelper.ExecuteDataReaderByProcedure(procedureName);
            while (dataReader.Read())
            {
                var user = new User
                {
                    userId = dataReader[1] as string,
                    password = dataReader[2] as string,
                    userName = dataReader[3] as string,
                    address = dataReader[4] as string,
                    email = dataReader[5] as string,
                    phone = dataReader[6] as string
                };


                //here I want to assign each object property as list element
                users.Add(user.GetPropertiesAuto());
            }
            return users;
        }
    }

Upvotes: 1

Jo&#227;o Paulo Amorim
Jo&#227;o Paulo Amorim

Reputation: 436

You can do it easy using a List of Users.

public class User
{
    public string userId { get; set; }
}
public List<User> ShowDetailsFromDB()
{
    using (adoHelper = new AdoHelper(connectionString))
    {
        List<User> users = new List<User>();
        string procedureName = "GetDetails";
        SqlDataReader dataReader = adoHelper.ExecuteDataReaderByProcedure(procedureName);
        while (dataReader.Read())
        {
            User user = new User
            {
                userId = dataReader[1] as string
            };

            users.Add(user);
            //here I want to assign each object property as list element

        }
        return users;
    }
}

Please tell me if it works

Upvotes: 0

Related Questions