Linq in not in\like not like

别人的博客 http://blog.163.com/lesheng@126/blog/static/357364652010102111051668/

using System.Data.Linq.SqlClient;//程序集 System.Data.Linq
string haveKey = Request.Form["haveKey"];
if (haveKey != null && haveKey.Length > 0)
{
    haveKey = string.Format("%{0}%", haveKey);
    list = from q in list
            where SqlMethods.Like(q.Name, haveKey)
            select q;
}
string noKey = Request.Form["noKey"];
if (noKey != null && noKey.Length > 0)
{
    noKey = string.Format("%{0}%", noKey);
    list = from q in list
            where !SqlMethods.Like(q.Name, noKey)
            select q;
}
-SQL的IN:

Select ProductID, ProductName, CategoryID
From dbo.Products
Where 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)

以下是我的实现:

GreenhouseBll ghBll = new IntelBLL.GreenhouseBll();
            List<GreenhouseModel> ghList = ghBll.GreenhouseUserSel(user.EnterpriseID, user.DistrictID, user.UserID);

            string noKey = "S%_S%";
            var expGh = from q in ghList
                       where !SqlMethods.Like(q.GreenhouseName, noKey)
                       select q;

            return Content("{\"total\":" + expGh.Count().ToString() + ",\"data\":" + new JavaScriptSerializer().Serialize(expGh.ToList()) + "}");

but,报错了:无法在客户端上使用方法“Boolean Like(System.String, System.String)”;它仅可用于转换为 SQL。┭┮﹏┭┮

看来我应该搜 linq to Entity not like

博客地址 https://www.cnblogs.com/mbailing/archive/2010/10/26/LinqToEntity.html

Linq to entity  indexof ()方法想当于  sql 中的like 语句,

但在  sql 中  like '010%' 是用来过滤以 '010' 开头的数据。

                  like '%010%' 是用来过滤 包含 010 的数据。

原本以为 linq to entity indexof()  方法能实现  like '010%'的效果,但是在实际中使用才发现,它其实实现的是  like '%010%'  的效果。

我的问题是,linq to entity 如何实现  sql 中 like '010%' 的查询效果,即查找开头为010的数据。

答案:用StartWith ()  方法可以实现  sql 中 like '010%'的效果,它是从开头字母做比对。

        indexof() 方法是 比对字符串中包含。

我的用法:like都出来了,not like 还远吗?

var expGh = from q in ghList
                        where q.GreenhouseName.IndexOf('S')==-1
                       select q;

            return Content("{\"total\":" + expGh.Count().ToString() + ",\"data\":" + new JavaScriptSerializer().Serialize(expGh.ToList()) + "}");

 原来的效果:

过滤后:

 原来记个博客也花不了多少时间嘛

猜你喜欢

转载自www.cnblogs.com/bamboo-zhang/p/9131994.html