C# 尝试获取具有相同 ID 但多个订单的数据

C# Trying to GET Data with the same ID but multiple orders

晚上好,

我在使用以下代码时遇到问题,它可以工作,但它只显示来自一个 ID 的一个订单:

namespace ProjectDatabase.Models
{
    public class searchQuery
    {
        public AppDb Db { get; }

        public searchQuery(AppDb db)
        {
            Db = db;
        }

        public async Task<search> FindAllAsync(int id)
        {
            using var cmd = Db.Connection.CreateCommand();
            cmd.CommandText = @"...My Query";
            cmd.Parameters.Add(new MySqlParameter
            {
                ParameterName = "@id_customer",
                DbType = DbType.Int32,
                Value = id,
            });
            var result = await ReadAllAsync(await cmd.ExecuteReaderAsync());
            return result.Count > 0 ? result[0] : null;
        }

        public async Task<search> FindOneIdAsync(int id)
        {
            using var cmd = Db.Connection.CreateCommand();
            cmd.CommandText = @"...My Query";
            cmd.Parameters.Add(new MySqlParameter
            {
                ParameterName = "@id_customer",
                DbType = DbType.Int32,
                Value = id,
            });
            var result = await ReadAllAsync(await cmd.ExecuteReaderAsync());
            return result.Count > 0 ? result[0] : null;
        }

       
        public async Task<List<search>> LatestSearchAsync()
        {
            using var cmd = Db.Connection.CreateCommand();
            cmd.CommandText = @"... My Query";
            return await ReadAllAsync(await cmd.ExecuteReaderAsync());
        }

        private async Task<List<search>> ReadAllAsync(DbDataReader reader)
        {
            var adb_search = new List<search>();
            using (reader)
            {
                while (await reader.ReadAsync())
                {
                    var search = new search(Db)
                    {
                        id_customer = reader.GetInt32(0),
                        delivery_person_name = reader.GetString(1),
                        firstname = reader.GetString(2),
                        lastname = reader.GetString(3),
                        email = reader.GetString(4),
                        order_number = reader.GetString(5),
                    };
                    adb_search.Add(search);
                }
            }
            return adb_search;
        }
    }
}

这是将在 Postman 中显示的内容:

{
    "id_customer": 88,
    "delivery_person_name": "Name",
    "firstname": "firstname",
    "lastname": "lastname",
    "email": "email",
    "order_number": "order"
}

问题是这个 ID 为 88 的客户将完成更多订单,例如 50 个,但它只显示第一个订单,仅此而已。结束,其他49个订单不显示

这很奇怪,因为我在这 3 个查询中有另一个查询,它向我显示数据库中的所有用户,它工作正常并向我显示所有 100 个用户,但是对于订单查询,它只向我显示 1 个结果,仅此而已.

也许问题是我使用相同的结果来显示 2 个不同的查询?在你问之前,是的,我已经在 SQLyog 中测试了查询,它显示了 50 个订单,但在代码中它只显示了第一个订单。

在你判断之前,是的,我是 C# 的菜鸟。

谢谢,NewSounD。

问题是 FindAllAsync return Task<search>。你想要一个列表,所以你需要 return Task<List<search>>.

这迫使您 return 只有一个 搜索 并编写此代码:return result.Count > 0 ? result[0] : null;.

固定码:

public async Task<List<search>> FindAllAsync(int id)
{
    using var cmd = Db.Connection.CreateCommand();
    cmd.CommandText = @"...My Query";
    cmd.Parameters.Add(new MySqlParameter
    {
        ParameterName = "@id_customer",
        DbType = DbType.Int32,
        Value = id,
    });
    var result = await ReadAllAsync(await cmd.ExecuteReaderAsync());
    return result;
}