c#linq填充非映射属性
我首先使用实体框架代码,我有两个实体:c#linq填充非映射属性,c#,entity-framework,linq-to-sql,C#,Entity Framework,Linq To Sql,我首先使用实体框架代码,我有两个实体: public class Product { public int ProductID { get; set; } public decimal Price { get; set; } public int ProductCategoryID { get; set; } public virtual ProductCategory ProductCategory { get; set; } [NotMa
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; }
}
我似乎无法获得填充有数量属性的产品
列表
我试过了
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
};
来自repository.Query()中的pr
pr.ProductID上的join st in repository.Query()等于st.Quantity
选择新产品()
{
ProductID=pr.ProductID,
....
数量=标准数量
};
但是这不起作用。您的加入在错误的字段上完成,请尝试以下操作:
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
};
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; } }
}