随笔 - 750  文章 - 1  评论 - 107  阅读 - 34万

[转]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 '李%')
             */
        }
    }
}
复制代码

 

posted on   z5337  阅读(279)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示