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);
  • 相关阅读:
    电容的用法:去耦、旁路、滤波等
    成为出色工程师的十大要素
    常用三极管的区别 9012 9013 9014 9015 8550 8050
    照明的几个光学概念
    PCB元件封装
    为什么诈骗短信看上去那么弱智
    摄像·镜头
    LED家居照明
    光色的应用与照度范围
    PowerPCB(PADS)常见问题全集
  • 原文地址:https://www.cnblogs.com/A_ming/p/2418962.html
Copyright © 2011-2022 走看看