Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将mysql查询中项的嵌套列表属性映射到c#对象_C#_Mysql_Sql_Json - Fatal编程技术网

将mysql查询中项的嵌套列表属性映射到c#对象

将mysql查询中项的嵌套列表属性映射到c#对象,c#,mysql,sql,json,C#,Mysql,Sql,Json,请在这里帮助我 我想获得产品的详细信息以及该产品的图片列表。下面的代码工作正常,但它只返回产品的一张图片。我在将MySQL查询结果映射到C#对象时遇到问题 这是我到目前为止所做的尝试 public ProductModel GetProductDetail(uint product_id) { var cmd = this.MySqlDatabase.Connection.CreateCommand() as MySqlCommand;

请在这里帮助我

我想获得产品的详细信息以及该产品的图片列表。下面的代码工作正常,但它只返回产品的一张图片。我在将MySQL查询结果映射到C#对象时遇到问题

这是我到目前为止所做的尝试

        public ProductModel GetProductDetail(uint product_id)
        {
            var cmd = this.MySqlDatabase.Connection.CreateCommand() as MySqlCommand;
            cmd.CommandText = @"SELECT products.*, images.* FROM products LEFT JOIN images ON products.product_id= images.product_id WHERE products.product_id = @product_id LIMIT 0,1";
            cmd.Parameters.AddWithValue("@product_id", product_id);
            using (var reader = cmd.ExecuteReader())
                while (reader.Read())
                {
                    return new ProductModel()
                    {
                        product_id = reader.GetValue<UInt32>("product_id"),
                        subcategory_id = reader.GetValue<UInt32>("subcategory_id"),
                        product_name = reader.GetValue<String>("product_name"),
                        description = reader.GetValue<String>("description"),
                        is_recent = reader.GetValue<Boolean>("is_recent"),
                        is_popular = reader.GetValue<Boolean>("is_popular"),
                        is_available = reader.GetValue<Boolean>("is_available"),
                        price = reader.GetValue<Decimal>("price"),
                        overall_rating = reader.GetValue<Double>("overall_rating"),
                        views = reader.GetValue<UInt32>("views"),
                        people_rated = reader.GetValue<UInt32>("people_rated"),
                        date_posted = reader.GetValue<DateTime>("date_posted"),
                        Images = new List<ImagesModel>
                        {
                            new ImagesModel
                            {
                                imageurl = reader.GetValue<String>("imageurl"),
                                created_date = reader.GetValue<DateTime>("created_date")
                            }
                        }
                    };
                }
            return null;
        }

请问我做错了什么?非常感谢您的帮助。

没有什么问题需要指出

问题

首先

SELECT products.*, images.* FROM products LEFT JOIN images ON products.product_id= images.product_id WHERE products.product_id = @product_id LIMIT 0,1
您的MYSQL查询包含
限制0,1
,这意味着它将只返回第一行

对该查询使用
左连接
并不正确,因为通过删除
限制0,1
,它返回1 x n个记录,其中n是具有相同
产品id的图像记录数

return new ProductModel()
{
    Images = new List<ImagesModel>
    {
        new ImagesModel
        {
            imageurl = reader.GetValue<String>("imageurl"),
            created_date = reader.GetValue<DateTime>("created_date")
        }
    }
};

为什么需要在一次查询中获取产品和图像?为了使单个对象更有效地在单独的查询中查询自身及其图像,这里没有选择N+1问题。非常令人印象深刻。竖起大拇指!!!还有一个问题,单独查询时是否存在性能问题?嗨@Pat,不客气。没有对代码进行测试,但总的来说,提到的这些概念可以帮助您理解问题,并提供解决问题的指南。希望你能尽快找到解决办法。谢谢,它像魔术一样奏效。你的回答使整个过程非常清楚
return new ProductModel()
{
    Images = new List<ImagesModel>
    {
        new ImagesModel
        {
            imageurl = reader.GetValue<String>("imageurl"),
            created_date = reader.GetValue<DateTime>("created_date")
        }
    }
};
public ProductModel GetProductDetail(uint product_id)
{
    ProductModel product = null;

    using (var productCmd = this.MySqlDatabase.Connection.CreateCommand() as MySqlCommand)
    {
        productCmd.CommandText = @"SELECT * FROM products WHERE product_id = @product_id";
        productCmd.Parameters.Add("@product_id", SqlDbType.int).Value = product_id;

        using (var reader = productCmd.ExecuteReader())
        {
            while (reader.Read())
            {
                product = new ProductModel()
                {
                    product_id = reader.GetValue<UInt32>("product_id"),
                    subcategory_id = reader.GetValue<UInt32>("subcategory_id"),
                    product_name = reader.GetValue<String>("product_name"),
                    description = reader.GetValue<String>("description"),
                    is_recent = reader.GetValue<Boolean>("is_recent"),
                    is_popular = reader.GetValue<Boolean>("is_popular"),
                    is_available = reader.GetValue<Boolean>("is_available"),
                    price = reader.GetValue<Decimal>("price"),
                    overall_rating = reader.GetValue<Double>("overall_rating"),
                    views = reader.GetValue<UInt32>("views"),
                    people_rated = reader.GetValue<UInt32>("people_rated"),
                    date_posted = reader.GetValue<DateTime>("date_posted")  
                };
            }
        }          
    }
           
    if (product == null)
        return null;

    var imageList = new List<ImagesModel>(); 
    using (var imageCmd = this.MySqlDatabase.Connection.CreateCommand() as MySqlCommand)
    {
        imageCmd.CommandText = @"SELECT * FROM images WHERE product_id = @product_id";
        imageCmd.Parameters.Add("@product_id", SqlDbType.int).Value = product_id;

        using (var reader = imageCmd.ExecuteReader())
        {
            while (reader.Read())
            {
                imageList.Add(new ImagesModel
                {
                    imageurl = reader.GetValue<String>("imageurl"),
                    created_date = reader.GetValue<DateTime>("created_date")
                });
            }
        }          
    }
    
    product.Images = imageList;    
    return product;
}