c# - 如何将 List<AnonymousType#1> 转换为 List<Model>?

标签 c#

在我的 MVC应用程序有 4 个模型类。

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; }  
}  

分类模型.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 - 具有上述三个类的所有必需属性的类。

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返回数据应为 Products_Categories_SuppliersModel 的类类型。

最佳答案

像这样写你的选择语句

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 };

关于c# - 如何将 List<AnonymousType#1> 转换为 List<Model>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44743522/

相关文章:

c# - 无法为当前正在执行的程序集中未定义的类型获取 Type 类的实例

c# - Regex.Split 不保留换行符

c# - Unity Survival Shooter Enemy 不受伤害 - Android

c# - ExceptionShielding 有更好的替代品吗?

C# 线程任务 - 无法从任务数组中获取返回值

c# - 将字符串从 datagridview 传递到另一种形式的文本框

c# - 在 C# 控制台应用程序中使用 HttpClient 使用 WEB API

c# - 如何为 AutoMapper 实现自定义命名解析器

c# - 正确的程序结构

c# - WCF 使用自定义 ApplicationPool Identity 慢 3 倍