我使用测试数据填充数据库,特别是客户端表。
我注意到,一旦我运行项目并且当它检测到客户端表为空时,它将在初始化程序例程中使用AddRange来添加100个客户端。
令我惊讶的是,我注意到系统添加100个客户端的方式与常规中列出的方式不一致。
这是它在初始化方法中的写法。
context.Clients.AddRange(
new Client { ABN = "", Active = true, Activity = true, BankAccount = "4308342999", BankBSB = "083-860", BankName = "NAB", ClientFirstName = "Christine", ClientLastName = "Green", ClientNo = 1001, Company = false, CompanyName = "", CreatorId = 1, DateCreated = new DateTime(2016, 02, 14), Email = "cgreen0@google.com.hk", MobilePhone = "0453439816", Phone = "354492968", Postcode = "3555", StateId = 2, Street1 = "01 Clemons Lane", Suburb = "Kangaroo Flat" },
new Client { ABN = "", Active = true, Activity = true, BankAccount = "5282113099", BankBSB = "012-857", BankName = "ANZ", ClientFirstName = "Jacqueline", ClientLastName = "Porter", ClientNo = 1002, Company = false, CompanyName = "", CreatorId = 1, DateCreated = new DateTime(2016, 02, 14), Email = "jporter1@prweb.com", MobilePhone = "0454972251", Phone = "354400440", Postcode = "3550", StateId = 2, Street1 = "336 Paget Crossing", Suburb = "Bendigo" },
new Client { ABN = "", Active = true, Activity = true, BankAccount = "6845784050", BankBSB = "083-625", BankName = "NAB", ClientFirstName = "Norma", ClientLastName = "Alvarez", ClientNo = 1003, Company = false, CompanyName = "", CreatorId = 1, DateCreated = new DateTime(2016, 02, 18), Email = "nalvarez2@telegraph.co.uk", MobilePhone = "0418263652", Phone = "354415087", Postcode = "3556", StateId = 2, Street1 = "9418 Ridge Oak Junction", Suburb = "Eaglenhawk" },.......
请注意,ClientNo以1000开头并递增。但是,当我检查数据库表的数据时,我得到了这个..
id ABN Active Activity BankAccount BankBSB BankName ClientFirstName ClientLastName ClientNo Company CompanyName CreatorId DateCreated Email MobilePhone Phone Postcode StateId Street1 Suburb
1 True True 4308342999 083-860 NAB Christine Green 1001 False 1 14/02/2016 12:00:00 AM 1/01/0001 12:00:00 AM 1/01/0001 12:00:00 AM cgreen0@google.com.hk 0453439816 NULL 354492968 3555 2 01 Clemons Lane NULL Kangaroo Flat
2 True True 1012236821 012-185 ANZ Patrick Rose 1073 False 1 2/10/2016 12:00:00 AM 1/01/0001 12:00:00 AM 1/01/0001 12:00:00 AM prose20@si.edu 0427959397 NULL 354416917 3556 2 3 Westport Crossing NULL Eaglenhawk
3 True True 9680901595 083-185 NAB Joan Perry 1072 False 1 23/09/2016 12:00:00 AM 1/01/0001 12:00:00 AM 1/01/0001 12:00:00 AM jperry1z@wsj.com 0445920808 NULL 354444482 3550 2 65944 Westport Crossing NULL Bendigo
ClientNo声明1000作为添加范围的一部分,每个客户端增加1但是您可以看到系统以不同的顺序添加了每个记录。
如何让AddRange按照它们在列表中出现的顺序添加记录?
答案 0 :(得分:0)
您可以随时将范围添加到列表中并按以下顺序排序:
var clientList = new List<Client>();
clientList.AddRange(
new Client { ABN = "", Active = true, Activity = true, BankAccount = "4308342999", BankBSB = "083-860", BankName = "NAB", ClientFirstName = "Christine", ClientLastName = "Green", ClientNo = 1001, Company = false, CompanyName = "", CreatorId = 1, DateCreated = new DateTime(2016, 02, 14), Email = "cgreen0@google.com.hk", MobilePhone = "0453439816", Phone = "354492968", Postcode = "3555", StateId = 2, Street1 = "01 Clemons Lane", Suburb = "Kangaroo Flat" },
new Client { ABN = "", Active = true, Activity = true, BankAccount = "5282113099", BankBSB = "012-857", BankName = "ANZ", ClientFirstName = "Jacqueline", ClientLastName = "Porter", ClientNo = 1002, Company = false, CompanyName = "", CreatorId = 1, DateCreated = new DateTime(2016, 02, 14), Email = "jporter1@prweb.com", MobilePhone = "0454972251", Phone = "354400440", Postcode = "3550", StateId = 2, Street1 = "336 Paget Crossing", Suburb = "Bendigo" },
new Client { ABN = "", Active = true, Activity = true, BankAccount = "6845784050", BankBSB = "083-625", BankName = "NAB", ClientFirstName = "Norma", ClientLastName = "Alvarez", ClientNo = 1003, Company = false, CompanyName = "", CreatorId = 1, DateCreated = new DateTime(2016, 02, 18), Email = "nalvarez2@telegraph.co.uk", MobilePhone = "0418263652", Phone = "354415087", Postcode = "3556", StateId = 2, Street1 = "9418 Ridge Oak Junction", Suburb = "Eaglenhawk" });
context.Clients.AddRange(clientList.OrderBy(c => c.ClientNo))