Hello everyone,
I building a MVC 4 project with Entity Framework 6. There is have business layer and data access layer also both are have its own model classes.
If I want to get list of table values from sql table -> DAL -> BAL -> Controller, which is the fastest and best way retrieve list from each other without using foreach and automapper.dll ?
Eg:
DAL-->
public class ProductsDAL
{
- public List<ProductCategory> GetProductCategoryTree()
- {
- using (var context = new ReboxEntities())
- {
- return context.ProductCategories.ToList();
- }
- }
- }
BAL-->
- public class ProductCategoryBL
- {
- public List<ProductCategoryDM> GetProductCategories()
- {
- ProductsDAL objProduct = new ProductsDAL();
- IList<ProductCategory> lsRes = new List<ProductCategory>();
- lsRes = objProduct.GetProductCategoryTree();
-
- List<ProductCategoryDM> categories = new List<ProductCategoryDM>();
- foreach (var item in lsRes)
- {
- categories.Add(new ProductCategoryDM{
- idProductCategory = item.idProductCategory,
- ProductCategoryName = item.ProductCategoryName,
- ProductCategoryDescription = item.ProductCategoryDescription,
- idParentCategoryProduct = item.idParentCategoryProduct,
- AlternateNamesToSearch = item.AlternateNamesToSearch,
- LevelID = item.LevelID,
- CompletePathName = item.CompletePathName,
- PCImage = item.PCImage
- });
- }
- return categories;
- }
then Controller,
-
- public class SearchVM
- {
- public List<ProductCategoryDM> Res = new List<ProductCategoryDM>();
- public SearchVM()
- {
- Res = new List<ProductCategoryDM>();
- }
- }
-
-
- [HttpPost]
- public ActionResult Search()
- {
- ProductCategoryBL objProduct = new ProductCategoryBL();
- List<ProductCategoryDM> lsRes = new List<ProductCategoryDM>();
- SearchVM vm = new SearchVM();
- lsRes = objProduct.GetProductCategories();
- vm.Res = lsRes;
- return PartialView("_searchresult", vm);
- }
Here is using too many foreach loop for mapping. How can I improve this? Please help me.