public ActionResult List(int cat_id) { List <Product> c = new List <Product>(); JoinSqlBuilder <Product, Product> jn = new JoinSqlBuilder <Product, Product>(); if (cat_id > 0) { //jn = jn.Join<Product, Product_In_Category>(m => m.Id, k => k.ProductId); jn = jn.Where <Product>(m => m.CatId == cat_id); } jn = jn.OrderBy <Product>(x => x.Order); var sql = jn.ToSql(); c = Db.Select <Product>(sql); var list_users = Cache_GetAllUsers(); var cats = Db.Select <Product_Category>(); foreach (var x in c) { var z = list_users.Where(m => m.Id == x.CreatedBy); if (z.Count() > 0) { var k = z.First(); if (string.IsNullOrEmpty(k.FullName)) { x.CreatedByUsername = k.UserName; } else { x.CreatedByUsername = k.FullName; } } else { x.CreatedByUsername = "******"; } var kk = cats.Where(q => q.Id == x.CatId).FirstOrDefault(); if (kk != null) { x.Category_Name = kk.Name; } else { x.Category_Name = "Deleted Category"; } } return(PartialView("_List", c)); }