• Entity Framework 4.0新增对TSQL的支持


    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 }

    后来加入了E-SQL,但还是和T-SQL有很大区别,我看到那一堆 as 和 it 就想给挖掉 :)

    EF 4.0新增加了对T-SQL的支持,根据T-SQL语句查询结果集中的列名自动映射到返回类型的属性名

    甚至连EDM文件不支持映射的sql_varent类型也可以自动转换了,代码如下:

    ExecuteStoreQuery方法
    protected void Page_Load(object sender, EventArgs e)
    {
    ERPEntities erp
    = new ERPEntities();
    string query = @" SELECT [BillPlanRule].[id] as BillID
    ,qu.UnitNumber
    ,[BeginDate]
    ,[EndDate]
    ,ft.value as FeeType
    ,f.value as Frequency
    ,[Price]
    ,[ActualPrice]
    ,[Fee]
    FROM [BillPlanRule]
    left join dbo.SysParameter as ft
    on [BillPlanRule].[FeeType]=ft.guid
    left join dbo.SysParameter as f
    on [BillPlanRule].[Frequency]=f.guid
    left join dbo.QuotationUnit as qu
    on [BillPlanRule].[UGuid]=qu.guid
    where uguid in (select guid from dbo.QuotationUnit
    where QuotationGuid in (select guid from dbo.Quotation where id=3) )
    order by BillID
    ";
    ObjectResult
    <SysParameter> result = erp.ExecuteStoreQuery<SysParameter>(query);
    List
    <SysParameter> list = result.ToList();
    }

    public class SysParameter
    {
    public int? BillID { get; set; }
    public DateTime? BeginDate { get; set; }
    public DateTime? EndDate { get; set; }
    public string FeeType { get; set; }
    public string Frequency { get; set; }
    public decimal? Price { get; set; }
    }

     

    其中FeeType和Frequency在数据库中的类型都为sql_varent

    ExecuteStoreQuery方法调用时并不是实时连接数据库查询,而是在下一句result.ToList()时才真正去查询

    此方法很适合做复杂查询和报表查询,如根据各种不同条件拼接sql字符串的查询

  • 相关阅读:
    工作中的几个问题
    linux初学之二
    VMWARE虚拟机卡死
    记昨天、今天的部分工作内容及问题
    linux初学之一
    今日阅读项目源码
    python POST XML
    python的超简单WEB服务器
    在notepad++中运行python
    安装python图形库:Matplotlib
  • 原文地址:https://www.cnblogs.com/luluping/p/1892651.html
Copyright © 2020-2023  润新知