1

我首先使用实体​​框架代码,我有两个实体:

public class Product 
{
    public int ProductID { get; set; }     
    public decimal Price { get; set; }
    public int ProductCategoryID { get; set; }
    public virtual ProductCategory ProductCategory { get; set; }

    [NotMapped]
    public int Quantity { get; set; }
} 

public class ProductStorage
{
  [Key]
  public int ProductId { get; set; }


  [ForeignKey("ProductId")]
  public virtual Product Product { get; set; }

  public int Quantity { get; set; }

}

我似乎无法获得Product充满该Quantity属性的列表。

我试过

from pr in repository.Query<Product>()
   join st in repository.Query<ProductStorage>() on pr.ProductID equals st.Quantity
   select new Product()
   {
       ProductID = pr.ProductID,
        ....
       Quantity = st.Quantity

   };

但这没有用。

4

1 回答 1

2

Join做错On了领域,试试这个:

from pr in repository.Query<Product>()
join st in repository.Query<ProductStorage>() on pr.ProductID equals st.ProductID 
select new Product()
   {
   ProductID = pr.ProductID,
        ....
   Quantity = st.Quantity
   };

但是,由于ProductStorageProduct共享该ProductId字段,这意味着您可以让它们以一对一的关系相关,例如:

public class Product 
{
    public int ProductID { get; set; }     
    public decimal Price { get; set; }
    public int ProductCategoryID { get; set; }
    public virtual ProductCategory ProductCategory { get; set; }
    public virtual ProductStorage Storage { get; set; }

    [NotMapped]
    public int Quantity { get { return this.Storage.Quantity; }  }
}
于 2013-05-24T07:51:55.550 回答