Entity Framework C# 输入数字并在 table 中返回匹配位置

Entity Framework C# Enter Number and and give back matching positions in a table

程序应首先提示用户通过 Console.Readline 输入客户编号。那么订单 table 中具有此客户编号的所有记录都将通过 ADO.NET.

读取和输出
    {
        static void Main(string[] args)
        {
            using (var ctx = new Sales())
            {
                Console.WriteLine("Enter OrderNo: ");
                int number = Convert.ToInt32(Console.ReadLine());
                
                foreach (var m in ctx.Order)
                {
                    Console.WriteLine($"{m.OrderNo}{m.Date}{m.CustomerNo}");
                }


            }
        }
    }

public class Sales : DbContext
        {
            public Sales() : base("name=Sales") { }
            public IDbSet<Order> Order { get; set; }
        }

        [Table("Order")]
        public class Order
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.None)]
            public int OrderNo { get; set; }
            public DateTime? Date { get; set; }
            public int CustomerNo { get; set; }
        }

我被困住了,伸出援助之手。

您错过了 Where 扩展方法:

ctx.Order.Where(o=>o.CustomerNo == number)

所以代码应该是这样的:

using (var ctx = new Sales())
            {
                Console.WriteLine("Enter OrderNo: ");
                int number = Convert.ToInt32(Console.ReadLine());
                
                foreach (var m in ctx.Order.Where(o=>o.CustomerNo == number))
                {
                    Console.WriteLine($"{m.OrderNo}{m.Date}{m.CustomerNo}");
                }
            }