• LINQ系列:LINQ to SQL Where条件


    1. 单一条件查询

    var expr = context.Products
        .Where(p => p.ProductName == "LINQ to SQL");
    SELECT 
        [Extent1].[ProductID] AS [ProductID], 
        [Extent1].[CategoryID] AS [CategoryID], 
        [Extent1].[ProductName] AS [ProductName], 
        [Extent1].[UnitPrice] AS [UnitPrice], 
        [Extent1].[Discontinued] AS [Discontinued]
        FROM [dbo].[Product] AS [Extent1]
        WHERE N'LINQ to SQL' = [Extent1].[ProductName]

    2. 组合条件查询

    var expr = context.Products
        .Where(p => p.ProductName == "LINQ to SQL" && p.UnitPrice > 10m);
    SELECT 
        [Extent1].[ProductID] AS [ProductID], 
        [Extent1].[CategoryID] AS [CategoryID], 
        [Extent1].[ProductName] AS [ProductName], 
        [Extent1].[UnitPrice] AS [UnitPrice], 
        [Extent1].[Discontinued] AS [Discontinued]
        FROM [dbo].[Product] AS [Extent1]
        WHERE (N'LINQ to SQL' = [Extent1].[ProductName]) AND ([Extent1].[UnitPrice] > cast(10 as decimal(18)))

    3. Like查询

    var expr = from p in context.Products
               where p.ProductName.StartsWith("LINQ")
               select new { p.ProductID, p.ProductName };
    SELECT 
        [Extent1].[ProductID] AS [ProductID], 
        [Extent1].[ProductName] AS [ProductName]
        FROM [dbo].[Product] AS [Extent1]
        WHERE [Extent1].[ProductName] LIKE N'LINQ%'
    var expr = from p in context.Products
               where p.ProductName.EndsWith("LINQ")
               select new { p.ProductID, p.ProductName };
    SELECT 
        [Extent1].[ProductID] AS [ProductID], 
        [Extent1].[ProductName] AS [ProductName]
        FROM [dbo].[Product] AS [Extent1]
        WHERE [Extent1].[ProductName] LIKE N'%LINQ'
    var expr = from p in context.Products
               where p.ProductName.Contains("LINQ")
               select new { p.ProductID, p.ProductName };
    SELECT 
        [Extent1].[ProductID] AS [ProductID], 
        [Extent1].[ProductName] AS [ProductName]
        FROM [dbo].[Product] AS [Extent1]
        WHERE [Extent1].[ProductName] LIKE N'%LINQ%'
  • 相关阅读:
    七牛云上传博客
    .net 导入Excel
    liunx ln -s 软连接
    dos2unix 命令
    x-csrf-token
    设置git 不提交 修改权限的文件
    nginx 启动、重启、关闭
    命令行导入mysql数据
    mongo 相关命令
    laravel 安装完成后安装 vendor 目录
  • 原文地址:https://www.cnblogs.com/libingql/p/4048520.html
Copyright © 2020-2023  润新知