zoukankan      html  css  js  c++  java
  • LINQ

    希望对大家在以后的项目中能用到,我也是在项目中碰到了这个问题;

    算算時間,接觸LINQ也有一個月的時間了,可以算是落伍兼新生,不過最近在寫專案的時候,遇到了在LINQ的Where條件式中要如何使用in與not in呢!? 這時候真的只能坐在位子上仰天長笑,開始懷念T-SQL其實你還是最好用滴。之後,為了讓自己日後開發時更為方便,於是花了一點時間,參考一些網路資料及MSDN後,得到以下的測試結果:
    T-SQL的IN: 
    Select ProductID, ProductName, CategoryID From dbo.Products  
    Where not CategoryID in (1, 2)

    T-SQL的NOT IN: 
    Select ProductID, ProductName, CategoryID From dbo.Products  
    Where CategoryID not in (1, 2) 
    or 
    Select ProductID, ProductName, CategoryID From dbo.Products  
    Where not CategoryID in (1, 2)

    LINQ的IN: 
    var queryResult = from p in db.Products 
    where (new int?[] {1,2}).Contains(p.CategoryID) 
    select p;

    LINQ的IN解析成SQL: 
    SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued] 
    FROM [dbo].[Products] AS [t0] 
    WHERE [t0].[CategoryID] IN (@p0, @p1)

    LINQ的NOT IN: 
    var queryResult = from p in db.Products 
    where !(new int?[] { 1, 2 }).Contains(p.CategoryID) 
    select p;

    LINQ的NOT IN解析成SQL: 
    SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued] 
    FROM [dbo].[Products] AS [t0] 
    WHERE NOT ([t0].[CategoryID] IN (@p0, @p1))

  • 相关阅读:
    差分隐私 differential privacy privSQL ||sql query ||sql查询系统||PrivateSQL:A Differentially Private SQL Query Engine论文笔记
    分冶法解决大整数相乘 最近对问题
    数论 矩阵交集
    STl 优先队列 C++
    备份mysql函数和存储过程
    Idea 注解模板
    excel导出
    帆软常用小技巧
    js + java文件下载
    try/finally
  • 原文地址:https://www.cnblogs.com/a-dou/p/5916895.html
Copyright © 2011-2022 走看看