Welcome

首页 / 软件开发 / .NET编程技术 / Entity Framework 4.0新增对T-SQL的支持

Entity Framework 4.0新增对T-SQL的支持2011-11-17 博客园 张磊EF4.0中新增了ExecuteStoreQuery,ExecuteStoreCommand,ExecuteFunction等方法,完美支持T- SQL

之前在EF中用Linq进行left join查询和报表数据查询简直是一场噩梦,以下代码就是例子:

Linq左连接查询N张表示例代码

1 public List<Info> getMapUnitList(int MapId)
2 {
3 var var1 = from mu in epm.MapUnit
4 join us in epm.UnitStatus
5 on mu.Unit equals us.Unit into _UnitStatus
6 join cu in epm.ContractUnit
7 on mu.Unit equals cu.Unit into _ContractUnit
8 where mu.Map.MapId == MapId
9 select new Info()
10 {
11 map = mu.Map
12 ,
13 mapstatus = null
14 ,
15 mapunit = mu
16 ,
17 unit = mu.Unit
18 ,
19 status = _UnitStatus.Select(a => a.Status).FirstOrDefault()
20 ,
21 contract = _ContractUnit.Select(a => a.Contract).FirstOrDefault()
22 ,
23 customer = null
24 ,
25 boothtype = null
26 };
27 var var2 = from v1 in var1
28 join ms in epm.MapStatus
29 on new { map = v1.map, status = v1.status }
30 equals new { map = ms.Map, status = ms.Status } into _MapStatus
31 select new Info()
32 {
33 map = null
34 ,
35 mapstatus = _MapStatus.FirstOrDefault()
36 ,
37 mapunit = v1.mapunit
38 ,
39 unit = v1.unit
40 ,
41 status = v1.status
42 ,
43 contract = v1.contract
44 ,
45 customer = null
46 ,
47 boothtype = null
48 };
49 var var3 = from v2 in var2
50 from ubtl in epm.UnitBoothType
51 where ubtl.Unit.UnitId == v2.unit.UnitId
52 join cc in epm.CustomerContract
53 on v2.contract equals cc.Contract into _CustomerContract
54 select new Info()
55 {
56 map = null
57 ,
58 mapstatus = v2.mapstatus
59 ,
60 mapunit = v2.mapunit
61 ,
62 unit = v2.unit
63 ,
64 status = v2.status
65 ,
66 contract = v2.contract
67 ,
68 customer = _CustomerContract.Select(a => a.Customer).FirstOrDefault()
69 ,
70 boothtype = ubtl.BoothType
71 };
72 List<Info> infos = var3.ToList();
73 return infos;
74 }