EntityFramework优于LinqToSQL的小知识点

Find方法:

 

         // Find方法
        
// 用于通过主键查找单个对象。
        
// 优点:Find会首先在Context的缓存中查找,如果查找到,则直接返回,避免的数据库的查询。FirstOrDefault会始终进行数据库查询,而不管缓存中是否已经存在
        Guid actionItemId = Guid.NewGuid();
        ActionItem actionItem = context.ActionItems.Find(actionItemId);

 

Include方法

         // Include方法(首先引用System.Data.Entity命名空间)
        
// 用于进行一次查询,便将相关的对象都一次性查找到。
        
// 优点:可极大地减少数据库查询次数,极大地提高页面性能。
        IEnumerable<ActionItem> actionItems = context.ActionItems
            .Include(item => item.Histories)
            .Include(item => item.ActionItemMembers)
            .Where(item => item.AssigneeId ==  this.User.Identity.Name);

         // 以下是MSDN对Include的用法的描述
        
// To include a single reference: query.Include(e => e.Level1Reference).
        
// To include a single collection: query.Include(e => e.Level1Collection).
        
// To include a reference and then a reference one level down: query.Include(e => e.Level1Reference.Level2Reference).
        
// To include a reference and then a collection one level down: query.Include(e => e.Level1Reference.Level2Collection).
        
// To include a collection and then a reference one level down: query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Reference)).
        
// To include a collection and then a collection one level down: query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Collection)).
        
// To include a collection, a reference, and a reference two levels down: query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Reference.Level3Reference)).
        
// To include a collection, a collection, and a reference two levels down: query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Collection.Select(l2 => l2.Level3Reference))).

 

EntityFunctions类

         // EntityFunctions类(首先引用System.Data.Objects命名空间)
        
// 有些Linq To Entity不支持的查询,需用EntityFunctions里的方法
        context.ActionItems.Where(item => EntityFunctions.TruncateTime(item.CreateTime) == DateTime.Today);

 

 

转载于:https://www.cnblogs.com/A_ming/archive/2012/03/27/2418962.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值