如何将 List<AnonymousType#1> 转换为 List<Model>?
How can I convert List<AnonymousType#1> to List<Model>?
在我的 MVC
应用程序中有 4 个模型 classes.
ProductModel.cs
public class ProductModel
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int SupplierID { get; set; }
public int CategoryID { get; set; }
}
CategoriesModel.cs
public class CategoriesModel
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
}
SuppliersModel.cs
public class SuppliersModel
{
public int SupplierID { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string Phone { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
Products_Categories_SuppliersModel - Class 具有上述三个 classes.
中的所有必需属性
public class Products_Categories_SuppliersModel
{
public string ProductName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string CompanyName { get; set; }
public string Phone { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
}
我在 List<ProductModel>
、List<CategoriesModel>
、List<SuppliersModel>
中有数据。使用 Linq
加入所有三个列表。
public List<Products_Categories_SuppliersModel> GetProduct_Category_SupplierData()
{
try
{
List<ProductModel> prodData = GetProductsData().ToList();
List<CategoriesModel> categData = GetCategoriesData().ToList();
List<SuppliersModel> supplData = GetSuppliersData();
var DBData = (from p in prodData
join c in categData
on p.CategoryID equals c.CategoryID
join s in supplData on p.SupplierID equals s.SupplierID
select new
{
p.ProductName,
c.CategoryName,
c.Description,
s.ContactName,
s.ContactTitle,
s.CompanyName,
s.Phone,
s.City,
s.Country
});
return DBData.ToList();
}
catch (Exception) { return null; }
finally {}
}
但是第 24 行抛出错误:
Cannot implicitly convert type
'System.Collections.Generic.List<AnonymousType#1>' to
'System.Collections.Generic.List<MVCApp.Models.Products_Categories_SuppliersModel>'.
上面的代码有什么错误?我创建了 Products_Categories_SuppliersModel
class 这样 return 数据应该是 Products_Categories_SuppliersModel
类型。
只需在您的 select 中实例化 Products_Categories_SuppliersModel
。
var DBData = (from p in prodData
join c in categData
on p.CategoryID equals c.CategoryID
join s in supplData on p.SupplierID equals s.SupplierID
select new Products_Categories_SuppliersModel()
{
p.ProductName,
c.CategoryName,
c.Description,
s.ContactName,
s.ContactTitle,
s.CompanyName,
s.Phone,
s.City,
s.Country
});
像这样写你的select声明
select new MVCApp.Models.Products_Categories_SuppliersModel
{
ProductName = p.ProductName,
CategoryName = c.CategoryName,
Description = c.Description,
ContactName = s.ContactName,
ContactTitle = s.ContactTitle,
CompanyName = s.CompanyName,
Phone = s.Phone,
City = s.City,
Country = s.Country };
在我的 MVC
应用程序中有 4 个模型 classes.
ProductModel.cs
public class ProductModel
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int SupplierID { get; set; }
public int CategoryID { get; set; }
}
CategoriesModel.cs
public class CategoriesModel
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
}
SuppliersModel.cs
public class SuppliersModel
{
public int SupplierID { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string Phone { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
Products_Categories_SuppliersModel - Class 具有上述三个 classes.
中的所有必需属性public class Products_Categories_SuppliersModel
{
public string ProductName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string CompanyName { get; set; }
public string Phone { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
}
我在 List<ProductModel>
、List<CategoriesModel>
、List<SuppliersModel>
中有数据。使用 Linq
加入所有三个列表。
public List<Products_Categories_SuppliersModel> GetProduct_Category_SupplierData()
{
try
{
List<ProductModel> prodData = GetProductsData().ToList();
List<CategoriesModel> categData = GetCategoriesData().ToList();
List<SuppliersModel> supplData = GetSuppliersData();
var DBData = (from p in prodData
join c in categData
on p.CategoryID equals c.CategoryID
join s in supplData on p.SupplierID equals s.SupplierID
select new
{
p.ProductName,
c.CategoryName,
c.Description,
s.ContactName,
s.ContactTitle,
s.CompanyName,
s.Phone,
s.City,
s.Country
});
return DBData.ToList();
}
catch (Exception) { return null; }
finally {}
}
但是第 24 行抛出错误:
Cannot implicitly convert type
'System.Collections.Generic.List<AnonymousType#1>' to
'System.Collections.Generic.List<MVCApp.Models.Products_Categories_SuppliersModel>'.
上面的代码有什么错误?我创建了 Products_Categories_SuppliersModel
class 这样 return 数据应该是 Products_Categories_SuppliersModel
类型。
只需在您的 select 中实例化 Products_Categories_SuppliersModel
。
var DBData = (from p in prodData
join c in categData
on p.CategoryID equals c.CategoryID
join s in supplData on p.SupplierID equals s.SupplierID
select new Products_Categories_SuppliersModel()
{
p.ProductName,
c.CategoryName,
c.Description,
s.ContactName,
s.ContactTitle,
s.CompanyName,
s.Phone,
s.City,
s.Country
});
像这样写你的select声明
select new MVCApp.Models.Products_Categories_SuppliersModel
{
ProductName = p.ProductName,
CategoryName = c.CategoryName,
Description = c.Description,
ContactName = s.ContactName,
ContactTitle = s.ContactTitle,
CompanyName = s.CompanyName,
Phone = s.Phone,
City = s.City,
Country = s.Country };