将匿名类型转换为IEnumerable <>

时间:2018-09-26 09:28:57

标签: linq-to-entities ienumerable anonymous

  

如何从 两个不同的表 中获取记录   (“产品”,“产品类别”)?

     

产品表包含( Id 名称价格 CateId
   Product_Category 表包含( Id Category CateId

   CateId Product_Category 表中的主键。

我的项目中有产品模型

public partial class Product
{        
    public int Id { get; set; }

    public string Name { get; set; }
    public decimal Price { get; set; }
    public string Category { get; set; }

    public Nullable<int> CateId { get; set; }
    [Display(Name = "Category")]
    public List<SelectListItem> categoryList { get; set; }      
}
  

通过使用以下代码,我得到了一个表的结果

public ActionResult DisplayProducts()
    {
        IEnumerable<Product> prodList = new List<Product>();
        prodList = db.Products.Select(x => new { x.Name, x.Price }).AsEnumerable()
            .Select(x => new Product { Name = x.Name, Price = x.Price });

        return View(prodList);
    }
  

结果如下

Result Image

我要显示 Product_Category

中的 Category

0 个答案:

没有答案
相关问题