Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private List<string> GetAllFieldValues(string p)
{
    List<string> field_values = new List<string>();
    string connectionString = ConfigurationManager.ConnectionStrings["SchoolContext"].ConnectionString;
    using (SqlConnection con = new SqlConnection(connectionString))
    {
        SqlCommand cmd = new SqlCommand();
        con.Open();
        cmd.CommandText = "SELECT " + p + " from tbl_student where institutionid=" + Convert.ToInt32(Session["institutionId"].ToString());
        cmd.Connection = con;
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                if (p == "dateofadmission" || p == "dateofbirth")
                    field_values.Add(Convert.ToDateTime(dr[0].ToString()).ToShortDateString());
                else
                    field_values.Add(dr[0].ToString());
            }
        }
        else
        {

        }
    }
    return field_values;
}


this is my ADO.NET code how can i convert this one to linq i have an idea to change linq but i don't know pass the parameter dynamically.
Posted
Updated 8-May-14 18:47pm
v3
Comments
Amalraj Ramesh 9-May-14 0:37am    
Not clear what do you want to do???
Prasad Avunoori 9-May-14 0:49am    
I think he wants LINQ equivalent of
" cmd.CommandText = "SELECT " + p + " from tbl_student where institutionid=" + Convert.ToInt32(Session["institutionId"].ToString());"
Prasad Avunoori 9-May-14 0:55am    
Hi Ilaya,

Please find the code below. I'm leaving where condition up to you.
ilaya muthumanickam 9-May-14 1:06am    
yes absolutely i want that Mr.Prasad i dont know how to modify that
ilaya muthumanickam 9-May-14 1:10am    
I already try this but i cant get the result Mr.Pradad if u have any alternative Logic please share...

1 solution

C#
string colName=p;
field_values = db.table1.Select(d=>new {d.colName}).Distinct().ToList();
 
Share this answer
 

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