• [转]MVC 经验总结_EF


    var s1 = context.Student.Where(o => o.ID > 0 && o.Name != "")
        .OrderByDescending(o => o.ID)
        .OrderBy(o => o.Name)
        .Select(o => new { o.Name, o.Class });
    s1 = from s in context.Student
            where s.ID > 0 && s.Name != ""
            orderby s.ID descending, s.Name ascending
            select new { s.Name, s.Class };

    两者是相等的。

    使用 IEnumerable<T与用 IQueryable<T> 不同,

    都有延迟加载,但前者会在内存执行,后者会先生成表达式树,查询由源对象处理。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleEFApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                StudyDBEntities context = new StudyDBEntities();
                /*
                // 增
                Student s1 = new Student();
                s1.Name = "张三";
                s1.Class = "101班";
                context.Student.Add(s1);
                context.SaveChanges();
                */
                /* 跟踪数据库操作为:
    INSERT [Student]([Name], [Class]) VALUES ('李四','103班')
    SELECT [ID] FROM [Student] WHERE @@ROWCOUNT > 0 AND [ID] = scope_identity()
                 */
                /*
                // 删
                int id = 1;
                var s3 = from s in context.Student
                         where s.ID == id
                         select s;
                context.Student.Remove(s3.FirstOrDefault());
                context.SaveChanges();
                */
                /*
    SELECT TOP (1) 
        [Extent1].[ID] AS [ID], 
        [Extent1].[Name] AS [Name], 
        [Extent1].[Class] AS [Class]
        FROM [dbo].[Student] AS [Extent1]
        WHERE [Extent1].[ID] = 1
    DELETE [dbo].[Student] WHERE ([ID] = 1)
                 */
    
                //
                int id = 2;
                var s3 = from s in context.Student
                         where s.ID == id
                         select s;
                var m = s3.FirstOrDefault();
                m.Name = "王五";
                context.SaveChanges();
                /*
    SELECT TOP (1) 
        [Extent1].[ID] AS [ID], 
        [Extent1].[Name] AS [Name], 
        [Extent1].[Class] AS [Class]
        FROM [dbo].[Student] AS [Extent1]
        WHERE [Extent1].[ID] = 2
    UPDATE [dbo].[Student] SET [Name] = '王五' WHERE ([ID] = 2)
                 */
                //// 1. 两者等同
                var s1 = context.Student.Select(s => s);
                foreach (var item in s1)
                {
                    Console.WriteLine(item.ID);
                }
                /* 跟踪数据库操作为:
    SELECT 
        [Extent1].[ID] AS [ID], 
        [Extent1].[Name] AS [Name], 
        [Extent1].[Class] AS [Class]
        FROM [dbo].[Student] AS [Extent1]             
                 */
                var s2 = from s in context.Student
                         where s.ID > 1 && s.Name.StartsWith("")
                         select s.Name;
                // 
                s2 = context.Student.Select(s => s.Name);            
                foreach (var item in s2)
                {
                    Console.WriteLine(item);
                }
                /* 跟踪数据库操作为:
    SELECT 
        [Extent1].[Name] AS [Name]
        FROM [dbo].[Student] AS [Extent1]
        WHERE ([Extent1].[ID] > 1) AND ([Extent1].[Name] LIKE '李%')
                 */
            }
        }
    }
  • 相关阅读:
    redis应用场景
    java.lang.IllegalArgumentException: Result Maps collection already contains value for xxx
    Java问题解决:Java compiler level does not match the version of the installed Java project facet.
    win10 安装Oracle 11g release 2
    Oracle 11G Client客户端安装
    Oracle分页查询排序数据重复问题
    Mysql 函数使用记录(三)——UNIX_TIMESTAMP() 、UNIX_TIMESTAMP(date)
    PL/SQL Developer过期解决方法
    PL/SQL Developer登录出现——Using a filter for all users can lead to poor performance!
    Oracle Single-Row Functions(单行函数)——NULL-Related Functions
  • 原文地址:https://www.cnblogs.com/z5337/p/5245829.html
走看看 - 开发者的网上家园