I'm trying to get the count of a column in a table using a SQL query to a C# list
my sample code is below
"Select count(1) as Values, SM.Name from SM INNER JOIN Bill on SM.ID = Bill.AL INNER JOIN Details on Bill.ID = Details.ID"
I need to to add the count to a list<> Can some one tell me how to do that?
Since this count value doesn't have a column name I have given it the name "Values" I have tried to get the value as following code
public IHttpActionResult Post(Brands brand)
{
DataTable dt = new DataTable();
List<Brands> list = new List<Brands>();
try
{
SqlConnection con =
new SqlConnection(
"MyConnectionString");
con.Open();
var query = "Select count(1) as Values, SM.Name from SM INNER JOIN Bill on SM.ID = Bill.AL INNER JOIN Details on Bill.ID = Details.ID";
SqlCommand com = new SqlCommand(query, con);
com.ExecuteNonQuery();
con.Close();
SqlDataAdapter adptr = new SqlDataAdapter(com);
adptr.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
Brands GetAll = new Brands();
GetAll.Count = Convert.ToInt32(dt.Rows[i]["Values"]);
GetAll.Name = dt.Rows[i]["Name"].ToString();
list.Add(GetAll);
}
}
catch (Exception e)
{
}
return Ok(list);
}
}
}