How to pass dynamic list to static list.
1. This return list
public List<Category> Category_All()
{
List<Category> AllCat = new List<Category>();
string constr = ConfigurationManager.ConnectionStrings["mysql"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
string query = "SELECT zutry_term_taxonomy.term_id as categoryid, zutry_terms.name as categoryname, zutry_terms.slug as url, zutry_term_taxonomy.parent as parentid FROM zutry_terms LEFT JOIN zutry_term_taxonomy ON zutry_terms.term_id = zutry_term_taxonomy.term_id WHERE zutry_term_taxonomy.taxonomy = 'product_cat'";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Connection = con;
con.Open();
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
AllCat.Add(new Category
{
category_id = int.Parse(sdr["categoryid"].ToString()),
category_name = sdr["categoryname"].ToString(),
urlpath = sdr["url"].ToString(),
parentid = int.Parse(sdr["parentid"].ToString())
//banner = sdr["image"].ToString()
});
}
}
con.Close();
}
}
return AllCat.ToList();
}
2. Model of above
public class Category
{
public int category_id { get; set; }
public string category_name { get; set; }
public string banner { get; set; }
public string urlpath { get; set; }
public int productid { get; set; }
public int parentid { get; set; }
}
/// this is staic list which pass in below method, intead of this static list i want to pass No 1 Method return list in Var generes
var genres = new[]
{
new Genre {Id = 1, Name = "Test Genre 1", ParentId = null},
new Genre {Id = 2, Name = "Test Genre 2", ParentId = null},
new Genre {Id = 3, Name = "Test Genre 3", ParentId = 1},
new Genre {Id = 4, Name = "Test Genre 4", ParentId = 2},
new Genre {Id = 5, Name = "Test Genre 5", ParentId = 3}
};
var result = genres.Select(g => SelectGenreName(genres, g));
///Method which take Ienumerable
private static string SelectGenreName(IEnumerable<Genre> all, Genre g)
{
var s = g.Name + '/';
if (g.ParentId.HasValue)
{
s = SelectGenreName(all, all.Single(x => x.Id == g.ParentId.Value));
}
return s;
}