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 關注
- 223 瀏覽
添加回答
舉報