为了账号安全,请及时绑定邮箱和手机立即绑定

用查询结果填充模型类

用查询结果填充模型类

C#
犯罪嫌疑人X 2023-12-17 17:04:06
我有以下模型 产品详细信息.cspublic 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()                    };       使用查询结果更新模型类的最佳方法是什么?
查看完整描述

3 回答

?
慕的地8271018

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)


查看完整回答
反对 回复 2023-12-17
?
DIEA

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);

        }


}


查看完整回答
反对 回复 2023-12-17
?
素胚勾勒不出你

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;

        }

    }

最后用这种方式就可以了


查看完整回答
反对 回复 2023-12-17
  • 3 回答
  • 0 关注
  • 85 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信