3 回答
TA贡献1796条经验 获得超4个赞
从 类中删除 lstProductDetails 属性。 ProductDetails
获取所有数据(结果的类型为:List<ProductDetails>)
var results = db.ProductInfoes
.Select(x => new ProductDetails()
{
ProductName = x.Product_Name.ToString(),
ProductCategoryID= x.Category_ID.ToString(),
ProductDescription = x.Product_Description.ToString()
}).ToList();
获取一条记录(结果的类型为:ProductDetails):
var result = db.ProductInfoes.FirstOrDefault(x => x.productCodeID.Equals(productCodeID))
.Select(x => new ProductDetails()
{
ProductName = x.Product_Name.ToString(),
ProductCategoryID= x.Category_ID.ToString(),
ProductDescription = x.Product_Description.ToString()
};
(添加命名空间System.Linq)
TA贡献1820条经验 获得超2个赞
您可以尝试扩展 ProductInfoes 并使用 automapper。
public **partial class** ProductInfoes {
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<AuthorModel, AuthorDTO>();
});
IMapper iMapper = config.CreateMapper();
public ProductDetails Map()
{
return iMapper.Map<ProductInfoes , ProductDetails>(this);
}
}
TA贡献1827条经验 获得超9个赞
public ProductDetails getSelectedProductDetails(string productCodeID)
{
try
{
NaturesKingdomUKEntities db = new NaturesKingdomUKEntities();
List<ProductInfo> lst_ProductInfo = db.ProductInfoes.Where(x => x.Product_Code_ID.Equals(productCodeID)).ToList();
ProductDetails prd = new ProductDetails();
prd = lst_ProductInfo
.Where(y => y.Product_Code_ID.Equals(productCodeID))
.Select(x => new ProductDetails { ProductName = x.Product_Name, ProductCodeID = x.Product_Code_ID, UnitPrice=Convert.ToDouble(x.Unit_Price), ProductDescription=x.Product_Description, ProductAdditionalDescription=x.Product_Additional_Description, ProductType=x.Product_Type })
.FirstOrDefault();
return prd;
}
catch (Exception ex)
{
throw;
}
}
最后用这种方式就可以了
- 3 回答
- 0 关注
- 155 浏览
添加回答
举报