zoukankan      html  css  js  c++  java
  • Entity Framework查询

    Entity Framework是个好东西,虽然没有Hibernate功能强大,但使用更简便。今天整理一下常见SQL如何用EF来表达,Func形式和Linq形式都会列出来(本人更喜欢Func形式)。

     1、简单查询:

    SQL:

    SELECT * FROM [Clients] WHERE Type=1 AND Deleted=0 ORDER BY ID 

    EF:

    //Func形式 
    var clients = ctx.Clients.Where(c => c.Type == 1 && c.Deleted == 0) 
                .OrderBy(c => c.ID) 
                .ToList(); 
     
    //Linq形式 
    var clients = from c in ctx.Clients 
                  where c.Type == 1 && c.Deleted==0 
                  orderby c.ID 
                  select c; 

    2、查询部分字段:

    SQL:

    SELECT ID,Name FROM [Clients] WHERE Status=1 

    EF:

    //Func形式 
    var clients = ctx.Clients.Where(c => c.Status == 1) 
                .Select(c => new { c.ID, Name = c.ComputerName }) 
                .ToList(); 
     
    //Linq形式 
    var clients = from c in ctx.Clients 
                  where c.Status == 1 
                  select new { c.ID, Name = c.ComputerName }; :

    3、查询单一记录:

    SQL:

    SELECT * FROM [Clients] WHERE ID=100 

    EF:

    //Func形式 
    var client = ctx.Clients.FirstOrDefault(c => c.ID == 100); 
     
    //Linq形式 
    var client = (from c in ctx.Clients 
                where c.ID = 100 
                select c).FirstOrDefault(); 

    4、LEFT JOIN 连接查询

    SQL:

    SELECT c.ID,c.ComputerName,g.Name GroupName  
    FROM [Clients] c  
    LEFT JOIN [Groups] g 
    ON c.GroupID = g.ID 
    WHERE c.Status=1 

    EF:

    //Func形式 
    var clients = ctx.Clients.Where(c => c.Status == 1) 
                .Select(c => new  
                { 
                    c.ID, 
                    c.ComputerName, 
                    GroupName = ctx.Groups.FirstOrDefault(g => g.ID == c.GroupID).Name 
                }) 
                .ToList(); 
     
    //Linq形式 
    var clients = from c in ctx.Clients 
                where c.Status == 1 
                select new 
                { 
                    c.ID, 
                    c.ComputerName, 
                    GroupName = (from g in ctx.Groups 
                                where g.ID == c.GroupID 
                                select g.Name).FirstOrDefault() 
                }; 

    5、INNER JOIN 连接查询:

    SQL:

    SELECT c.ID,c.ComputerName,g.Name GroupName  
    FROM [Clients] c 
    INNER JOIN [Groups] g 
    ON c.GroupID = g.ID 
    WHERE c.Status=1 
    ORDER BY g.Name 

    EF:

    /Func形式 
    var clients = ctx.Clients.Where(c => c.Status == 1) 
                .Join(ctx.Group, c => c.GroupID, g => g.ID, (c,g) =>  
                { 
                    c.ID, 
                    c.ComputerName, 
                    GroupName = g.Name 
                }) 
                .OrderBy(item => item.GroupName) 
                .ToList(); 
     
     
    //Linq形式1 
    var clients = from c in ctx.Clients 
                from g in ctx.Groups 
                where c.GroupID == g.ID 
                orderby g.Name 
                select new 
                { 
                    c.ID, 
                    c.ComputerName, 
                    GroupName = g.Name 
                }; 
     
    //Linq形式2 
    var clients = from c in ctx.Clients 
                where c.Status == 1 
                join g in ctx.Group 
                on c.GroupID equals g.ID into result 
                from r in result 
                order by r.Name 
                select new 
                { 
                    c.ID, 
                    c.ComputerName, 
                    GroupName = r.Name 
                }; 

    6、分页

    SQL:

    -- 方案1 
    SELECT TOP 10 * FROM [Clients] WHERE Status=1 
    AND ID NOT IN  
    ( 
        SELECT TOP 20 ID FROM [Clients] WHERE Status=1 
        ORDER BY ComputerName 
    ) 
    ORDER BY ComputerName 
     
    --方案2 
    SELECT * FROM 
    ( 
        SELECT *, ROW_NUMBER() OVER(ORDER BY ComputerName) AS RowNo 
        FROM [Clients] 
        WHERE Status=1 
    )t 
    WHERE RowNo >= 20 AND RowNo < 30 

    EF:

    //Func形式 
    var clients = ctx.Clients.Where(c => c.Status=1) 
                .OrderBy(c => c.ComputerName) 
                .Skip(20) 
                .Take(10) 
                .ToList(); 
     
    //Linq形式 
    var clients = (from c in ctx.Clients 
                orderby c.ComputerName 
                select c).Skip(20).Take(10); 

    7、分组统计:

    SQL:

    SELECT Status,COUNT(*) AS Cnt FROM [Clients]  
    GROUP BY Status 
    ORDER BY COUNT(*) DESC 

    EF:

    //Func形式 
    var result = ctx.Clients.GroupBy(c => c.Status) 
                .Select(s => new 
                { 
                    Status = s.Key, 
                    Cnt = s.Count() 
                }) 
                .OrderByDescending(r => r.Cnt); 
     
    //Linq形式 
    var result = from c in ctx.Clients 
                group c by c.Status into r 
                orderby r.Count() descending 
                select new 
                { 
                    Status = r.Key, 
                    Cnt = r.Count() 
                }; 

    1、简单查询:

    SQL:

    1. SELECT * FROM [Clients] WHERE Type=1 AND Deleted=0 ORDER BY ID 

    EF:

    1. //Func形式 
    2. var clients = ctx.Clients.Where(c => c.Type == 1 && c.Deleted == 0) 
    3.             .OrderBy(c => c.ID) 
    4.             .ToList(); 
    5.  
    6. //Linq形式 
    7. var clients = from c in ctx.Clients 
    8.               where c.Type == 1 && c.Deleted==0 
    9.               orderby c.ID 
    10.               select c; 


    2、查询部分字段:

    SQL:

    1. SELECT ID,Name FROM [Clients] WHERE Status=1 

    EF:

    1. //Func形式 
    2. var clients = ctx.Clients.Where(c => c.Status == 1) 
    3.             .Select(c => new { c.ID, Name = c.ComputerName }) 
    4.             .ToList(); 
    5.  
    6. //Linq形式 
    7. var clients = from c in ctx.Clients 
    8.               where c.Status == 1 
    9.               select new { c.ID, Name = c.ComputerName }; 


    3、查询单一记录:

    SQL:

    1. SELECT * FROM [Clients] WHERE ID=100 

    EF:

    1. //Func形式 
    2. var client = ctx.Clients.FirstOrDefault(c => c.ID == 100); 
    3.  
    4. //Linq形式 
    5. var client = (from c in ctx.Clients 
    6.             where c.ID = 100 
    7.             select c).FirstOrDefault(); 


    4、LEFT JOIN 连接查询

    SQL:

    1. SELECT c.ID,c.ComputerName,g.Name GroupName  
    2. FROM [Clients] c  
    3. LEFT JOIN [Groups] g 
    4. ON c.GroupID = g.ID 
    5. WHERE c.Status=1 

    EF:

    1. //Func形式 
    2. var clients = ctx.Clients.Where(c => c.Status == 1) 
    3.             .Select(c => new  
    4.             { 
    5.                 c.ID, 
    6.                 c.ComputerName, 
    7.                 GroupName = ctx.Groups.FirstOrDefault(g => g.ID == c.GroupID).Name 
    8.             }) 
    9.             .ToList(); 
    10.  
    11. //Linq形式 
    12. var clients = from c in ctx.Clients 
    13.             where c.Status == 1 
    14.             select new 
    15.             { 
    16.                 c.ID, 
    17.                 c.ComputerName, 
    18.                 GroupName = (from g in ctx.Groups 
    19.                             where g.ID == c.GroupID 
    20.                             select g.Name).FirstOrDefault() 
    21.             }; 


    5、INNER JOIN 连接查询:

    SQL:

    1. SELECT c.ID,c.ComputerName,g.Name GroupName  
    2. FROM [Clients] c 
    3. INNER JOIN [Groups] g 
    4. ON c.GroupID = g.ID 
    5. WHERE c.Status=1 
    6. ORDER BY g.Name 

    EF:

    1. //Func形式 
    2. var clients = ctx.Clients.Where(c => c.Status == 1) 
    3.             .Join(ctx.Group, c => c.GroupID, g => g.ID, (c,g) =>  
    4.             { 
    5.                 c.ID, 
    6.                 c.ComputerName, 
    7.                 GroupName = g.Name 
    8.             }) 
    9.             .OrderBy(item => item.GroupName) 
    10.             .ToList(); 
    11.  
    12.  
    13. //Linq形式1 
    14. var clients = from c in ctx.Clients 
    15.             from g in ctx.Groups 
    16.             where c.GroupID == g.ID 
    17.             orderby g.Name 
    18.             select new 
    19.             { 
    20.                 c.ID, 
    21.                 c.ComputerName, 
    22.                 GroupName = g.Name 
    23.             }; 
    24.  
    25. //Linq形式2 
    26. var clients = from c in ctx.Clients 
    27.             where c.Status == 1 
    28.             join g in ctx.Group 
    29.             on c.GroupID equals g.ID into result 
    30.             from r in result 
    31.             order by r.Name 
    32.             select new 
    33.             { 
    34.                 c.ID, 
    35.                 c.ComputerName, 
    36.                 GroupName = r.Name 
    37.             }; 


    6、分页

    SQL:

    1. -- 方案1 
    2. SELECT TOP 10 * FROM [Clients] WHERE Status=1 
    3. AND ID NOT IN  
    4.     SELECT TOP 20 ID FROM [Clients] WHERE Status=1 
    5.     ORDER BY ComputerName 
    6. ORDER BY ComputerName 
    7.  
    8. --方案2 
    9. SELECT * FROM 
    10.     SELECT *, ROW_NUMBER() OVER(ORDER BY ComputerName) AS RowNo 
    11.     FROM [Clients] 
    12.     WHERE Status=1 
    13. )t 
    14. WHERE RowNo >= 20 AND RowNo < 30 

    EF:

    1. //Func形式 
    2. var clients = ctx.Clients.Where(c => c.Status=1) 
    3.             .OrderBy(c => c.ComputerName) 
    4.             .Skip(20) 
    5.             .Take(10) 
    6.             .ToList(); 
    7.  
    8. //Linq形式 
    9. var clients = (from c in ctx.Clients 
    10.             orderby c.ComputerName 
    11.             select c).Skip(20).Take(10); 


    7、分组统计:

    SQL:

    1. SELECT Status,COUNT(*) AS Cnt FROM [Clients]  
    2. GROUP BY Status 
    3. ORDER BY COUNT(*) DESC 

    EF:

    1. //Func形式 
    2. var result = ctx.Clients.GroupBy(c => c.Status) 
    3.             .Select(s => new 
    4.             { 
    5.                 Status = s.Key, 
    6.                 Cnt = s.Count() 
    7.             }) 
    8.             .OrderByDescending(r => r.Cnt); 
    9.  
    10. //Linq形式 
    11. var result = from c in ctx.Clients 
    12.             group c by c.Status into r 
    13.             orderby r.Count() descending 
    14.             select new 
    15.             { 
    16.                 Status = r.Key, 
    17.                 Cnt = r.Count() 
    18.             }; 
  • 相关阅读:
    消息中间件(一)MQ详解及四大MQ比较
    WebSocket 详解教程
    Nginx 简易教程
    排序七 归并排序
    排序五 简单选择排序
    排序四 希尔排序
    排序二 快速排序
    排序一 冒泡排序
    [算法题] 人民币大小写转换(阿拉伯数字和汉字转换)
    Linux编程 18 安装软件程序(yum工具对软件包安装,删除,更新介绍)
  • 原文地址:https://www.cnblogs.com/coce/p/9456228.html
Copyright © 2011-2022 走看看