1+ using Microsoft . AspNetCore . Mvc ;
2+ using Microsoft . AspNetCore . Mvc . RazorPages ;
3+ using System . Dynamic ;
4+ namespace DataGrid . Pages
5+ {
6+ //public class OrderDetails
7+ //{
8+ // public static List<OrderDetails> order = new List<OrderDetails>();
9+
10+ // public OrderDetails(int OrderID, string CustomerId, int EmployeeId, double Freight,
11+ // DateTime OrderDate, string ShipCity)
12+ // {
13+ // this.OrderID = OrderID;
14+ // this.CustomerID = CustomerId;
15+ // this.EmployeeID = EmployeeId;
16+ // this.Freight = Freight;
17+ // this.ShipCity = ShipCity;
18+ // this.OrderDate = OrderDate;
19+ // }
20+
21+ // public static List<OrderDetails> GetAllRecords()
22+ // {
23+ // if (order.Count() == 0)
24+ // {
25+ // int code = 100;
26+ // for (int i = 1; i < 10; i++)
27+ // {
28+ // order.Add(new OrderDetails(code + 1, "ALFKI", i + 0, 2.3 * i, new DateTime(1991, 05, 15), "Berlin"));
29+ // order.Add(new OrderDetails(code + 2, "ANATR", i + 2, 3.3 * i, new DateTime(1990, 04, 04), "Madrid"));
30+ // order.Add(new OrderDetails(code + 3, "ANTON", i + 1, 4.3 * i, new DateTime(1957, 11, 30), "Cholchester"));
31+ // order.Add(new OrderDetails(code + 4, "BLONP", i + 3, 5.3 * i, new DateTime(1930, 10, 22), "Marseille"));
32+ // order.Add(new OrderDetails(code + 5, "BOLID", i + 4, 6.3 * i, new DateTime(1953, 02, 18), "Tsawassen"));
33+ // order.Add(new OrderDetails(code + 6, "Vinoth", i + 4, 6.3 * i, new DateTime(1953, 02, 18), "Berlin"));
34+ // code += 5;
35+ // }
36+ // }
37+ // return order;
38+ // }
39+
40+ // public int OrderID { get; set; }
41+ // public string CustomerID { get; set; }
42+ // public int? EmployeeID { get; set; }
43+ // public double? Freight { get; set; }
44+ // public string ShipCity { get; set; }
45+ // public DateTime OrderDate { get; set; }
46+
47+ //}
48+ public class DynamicDictionary : DynamicObject
49+ {
50+ public Dictionary < string , object > OrderDictionary { get ; set ; } = new Dictionary < string , object > ( ) ;
51+
52+ public override bool TryGetMember ( GetMemberBinder binder , out object ? result )
53+ {
54+ string name = binder . Name ;
55+ return OrderDictionary . TryGetValue ( name , out result ) ;
56+ }
57+
58+ public override bool TrySetMember ( SetMemberBinder binder , object value )
59+ {
60+ OrderDictionary [ binder . Name ] = value ;
61+ return true ;
62+ }
63+
64+ public override IEnumerable < string > GetDynamicMemberNames ( )
65+ {
66+ return OrderDictionary . Keys ;
67+ }
68+ }
69+ public class IndexModel : PageModel
70+ {
71+ public static List < DynamicDictionary > DynamicOrders { get ; set ; } = new List < DynamicDictionary > ( ) ;
72+ public void OnGet ( )
73+ {
74+ DynamicOrders = Enumerable . Range ( 1 , 10 ) . Select ( ( x ) =>
75+ {
76+ dynamic dynamicObj = new DynamicDictionary ( ) ;
77+ dynamicObj . OrderID = 1000 + x ;
78+ dynamicObj . CustomerID = ( new string [ ] { "ALFKI" , "ANANTR" , "ANTON" , "BLONP" , "BOLID" , "Thomp" } ) [ new Random ( ) . Next ( 5 ) ] ;
79+ dynamicObj . Freight = ( new double [ ] { 2 , 1 , 4 , 5 , 3 } ) [ new Random ( ) . Next ( 5 ) ] * x ;
80+ dynamicObj . OrderDate = ( new DateTime [ ] { new DateTime ( 2010 , 11 , 5 ) , new DateTime ( 2018 , 10 , 3 ) , new DateTime ( 1995 , 9 , 9 ) , new DateTime ( 2012 , 8 , 2 ) , new DateTime ( 2015 , 4 , 11 ) } ) [ new Random ( ) . Next ( 5 ) ] ;
81+ dynamicObj . ShipCity = ( new string [ ] { "Berlin" , "Madrid" , "Cholchester" , "Marseille" , "Tsawassen" , "Berlin" } ) [ new Random ( ) . Next ( 5 ) ] ;
82+ return dynamicObj ;
83+ } ) . Cast < DynamicDictionary > ( ) . ToList < DynamicDictionary > ( ) ;
84+ }
85+ }
86+ }
0 commit comments