猿问

用查询结果填充模型类

我有以下模型 产品详细信息.cs


public class ProductDetails

{

    public string ProductName { get; set; }


    public string ProductDescription { get; set; }


    public string ProductCodeID { get; set; }


    public string CategoryName { get; set; }


    public List<ProductDetails> lstProductDetails { get; set; }


}

需要用我从数据库获得的查询结果填充这个模型类


这就是我尝试过的..


    var results = from a in db.ProductInfoes where productCodeID.Equals(productCodeID)

                    select new ProductDetails

                    {

                        ProductName = a.Product_Name.ToString(),

                        ProductCategoryID= a.Category_ID.ToString(),

                        ProductDescription = a.Product_Description.ToString()

                    };       

使用查询结果更新模型类的最佳方法是什么?


犯罪嫌疑人X
浏览 157回答 3
3回答

慕的地8271018

从 类中删除 lstProductDetails 属性。 ProductDetails获取所有数据(结果的类型为:List<ProductDetails>)var results = db.ProductInfoes&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Select(x => new ProductDetails()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProductName = x.Product_Name.ToString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProductCategoryID= x.Category_ID.ToString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProductDescription = x.Product_Description.ToString()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).ToList();&nbsp;&nbsp;获取一条记录(结果的类型为:ProductDetails):var result = db.ProductInfoes.FirstOrDefault(x => x.productCodeID.Equals(productCodeID))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Select(x => new ProductDetails()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProductName = x.Product_Name.ToString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProductCategoryID= x.Category_ID.ToString(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProductDescription = x.Product_Description.ToString()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp;&nbsp;(添加命名空间System.Linq)

DIEA

您可以尝试扩展 ProductInfoes 并使用 automapper。public **partial class** ProductInfoes {var config = new MapperConfiguration(cfg => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cfg.CreateMap<AuthorModel, AuthorDTO>();&nbsp; &nbsp; &nbsp; &nbsp; });IMapper iMapper = config.CreateMapper();&nbsp; &nbsp; &nbsp; &nbsp; public ProductDetails Map()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return iMapper.Map<ProductInfoes , ProductDetails>(this);&nbsp; &nbsp; &nbsp; &nbsp; }}

素胚勾勒不出你

&nbsp; &nbsp; public ProductDetails getSelectedProductDetails(string productCodeID)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NaturesKingdomUKEntities db = new NaturesKingdomUKEntities();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<ProductInfo> lst_ProductInfo = db.ProductInfoes.Where(x => x.Product_Code_ID.Equals(productCodeID)).ToList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProductDetails prd = new ProductDetails();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prd = lst_ProductInfo&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.Where(y => y.Product_Code_ID.Equals(productCodeID))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.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 })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.FirstOrDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return prd;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }最后用这种方式就可以了
随时随地看视频慕课网APP
我要回答