L A

L A

  • NA
  • 170
  • 171.1k

Can't convert List<AnonymousType#1> to List<Model>

Jun 24 2017 12:23 PM
Hello all,
 
In my ASP.NETMVC application have 4 model classes.
 
ProductModel
  1. public class ProductModel  
  2.     {  
  3.         public int ProductID { getset; }  
  4.         public string ProductName { getset; }  
  5.         public int SupplierID { getset; }  
  6.         public int CategoryID { getset; }  
  7.     } 
CategoriesModel
  1. public class CategoriesModel  
  2.     {  
  3.         public int CategoryID { getset; }  
  4.         public string CategoryName { getset; }  
  5.         public string Description { getset; }  
  6.     } 
SuppliersModel
  1. public class SuppliersModel  
  2.     {  
  3.         public int SupplierID { getset; }  
  4.         public string CompanyName { getset; }  
  5.         public string ContactName { getset; }  
  6.         public string ContactTitle { getset; }  
  7.         public string Phone { getset; }  
  8.         public string City { getset; }  
  9.         public string Country { getset; }  
  10.     } 
Products_Categories_SuppliersModel - Class with all the required properties from above three classes.
  1. public class Products_Categories_SuppliersModel  
  2.     {  
  3.         public string ProductName { getset; }  
  4.         public string ContactName { getset; }  
  5.         public string ContactTitle { getset; }  
  6.         public string CompanyName { getset; }  
  7.         public string Phone { getset; }  
  8.         public string City { getset; }  
  9.         public string Country { getset; }  
  10.         public string CategoryName { getset; }  
  11.         public string Description { getset; }  
  12.     } 
I have data in List<ProductModel>, List<CategoriesModel>, List<SuppliersModel>. Used LINQ to join all three Lists<>.
  1. public List<Products_Categories_SuppliersModel> GetProduct_Category_SupplierData()  
  2. {  
  3.     try  
  4.     {  
  5.         List<ProductModel> prodData = GetProductsData().ToList();  
  6.         List<CategoriesModel> categData = GetCategoriesData().ToList();  
  7.         List<SuppliersModel> supplData = GetSuppliersData();  
  8.         var DBData = (from p in prodData  
  9.                      join c in categData  
  10.                      on p.CategoryID equals c.CategoryID  
  11.                      join s in supplData on p.SupplierID equals s.SupplierID  
  12.                      select new  
  13.                      {  
  14.                          p.ProductName,  
  15.                          c.CategoryName,  
  16.                          c.Description,  
  17.                          s.ContactName,  
  18.                          s.ContactTitle,  
  19.                          s.CompanyName,  
  20.                          s.Phone,  
  21.                          s.City,  
  22.                          s.Country  
  23.                      });  
  24.         return DBData.ToList();  
  25.     }  
  26.     catch (Exception) { return null; }  
  27.     finally {}  

But Line # 24 throws error - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<MVCApp.Models.Products_Categories_SuppliersModel>'.
 
What's the mistake in above code.? I have created Products_Categories_SuppliersModel class such that return data should be of 'Products_Categories_SuppliersModel' type.
Please suggest me to figure out my mistakes & help me code in a better way.
 
Thanks in advance.

Answers (5)