1 LightSpeed中使用子表达式
2
3 Query query = new Query();
4 //创建一个求和的子表达式
5 query.Subexpressions.Add("TotalCost",
6 Entity.Attribute("AllowAdd").Function("SUM"),
7 "Id");
8 query.Projection.Add(Entity.Attribute("AllowExport").Function("Sun"));
9 //将子表达式作为查询条件
10 query.QueryExpression = Entity.Attribute("TotalCost") > 0;
11
12 IList<RolePerm> ordersOver10000 = scop.Current.Find<RolePerm>(query);
13 //得到一个DataReader
14 using (var reader = scop.Current.Project(query))
15 {
16 if (reader.Read())
17 {
18 int num = Convert.ToInt32(reader["AllowExport"]) > 0 ? 1 : 0;
19 }
20 }
21
22 LightSpeed中的Join
23
24 Query query = new Query
25 {
26 // Join on Customer.City = Shipper.City
27 Join = Join.Inner<Customer, Shipper>("City", "City")
28 };
29
30 EntityTuple joinedResults = UnitOfWork.Find(query, query.Mappings);
31
32 IList<Customer> customers = joinedResults.GetCollection<Customer>();
33 IList<Shipper> shippers = joinedResults.GetCollection<Shipper>();
34
35 // Each index into the collections is a pair of entities that
36 // meet the join condition.
37 for (int i = 0; i < customers.Count; ++i)
38 {
39 Console.WriteLine("Customer {0} is in the same city as shipper {1}", customers[i].Name, shippers[i].Name);
40 }
41
42 Join多连接查询将一个实体代替另一个实体
43
44 internal IList<Customer> GetCustomersInCitiesThatHaveHadShipmentsSince(DateTime since)
45 {
46 Query subquery = new Query(typeof(Order))
47 {
48 Join = Join.Inner<Order, Shipper>("ShipperId", "Id"),
49 QueryExpression = Entity.Attribute("ShipDate") >= since
50 };
51
52 Query query = new Query(typeof(Customer))
53 {
54 Join = Join.Inner(typeof(Customer), subquery, "City", "Shipper.City")
55 };
56
57 return UnitOfWork.Find<Customer>(query);
58 }
59
60
61 LightSpeed中的Group
62
63 Query anyQuery = new Query(typeof(Customer))
64 {
65 Group = Group.By(new IdentifierExpression("City")).WhereAnyMatches(),
66 QueryExpression = Entity.Attribute("Balance") < 0
67 };
68
69 using (IDataReader reader = UnitOfWork.Project(anyQuery))
70 {
71 while (reader.Read())
72 {
73 Console.WriteLine("The city {0} contains 1 or more debtors", reader[0]);
74 }
75 }
76
77 Query allQuery = new Query(typeof(Customer))
78 {
79 Group = Group.By(new IdentifierExpression("City")).WhereAllMatch(),
80 QueryExpression = Entity.Attribute("Balance") < 0
81 };
82
83 using (IDataReader reader = UnitOfWork.Project(allQuery))
84 {
85 while (reader.Read())
86 {
87 Console.WriteLine("The city {0} contains only debtors", reader[0]);
88 }
89 }
90
91
92 简单的表达式查询
93 using (var unitOfWork = _context.CreateUnitOfWork())
94 {
95 // find all Contributions with a title of "Dolphins"
96
97 unitOfWork.Find<Contribution>(Entity.Attribute("Title") == "Dolphins");
98
99 // find all Contributions with a title of "Dolphins"
100 // that were added after 01-01-2007
101
102 unitOfWork.Find<Contribution>(
103 Entity.Attribute("Title") == "Dolphins"
104 && Entity.Attribute("AddedOn") > new DateTime(2007, 01, 01));
105 }