zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    java学习day08--面向对象--继承+方法重写+static关键字
    java学习day07--面向对象--封装+this关键字+构造器
    java学习day06-面向对象--类和对象
    依赖管理
    NSQ消息队列
    logger包
    time包
    fmt包
    Go_Protobu
    Go_性能优化
  • 原文地址:https://www.cnblogs.com/A_ming/p/2418962.html
Copyright © 2011-2022 走看看