复制代码 代码如下:
select * from dbo.Product
where
(ProductName like 'Product1%' or
ProductName like 'Product2%')
复制代码 代码如下:
select * from dbo.Product
where
ProductName like 'Product1%'
UNION
select * from DocutapCMS.dbo.Product
where
ProductName like 'Product2%'
复制代码 代码如下:
public static Expression<Func<TElement, bool>> BuildContainsExpression<TElement, TValue>(Expression<Func<TElement, TValue>> valueSelector,
IEnumerable<TValue> values)
{
var startsWithMethod = typeof (string).GetMethod("StartsWith", new[] { typeof(string) });
var startWiths = values.Select(value => (Expression)Expression.Call(valueSelector.Body, startsWithMethod, Expression.Constant(value, typeof(TValue))));
var body = startWiths.Aggregate<Expression>(((accumulate, equal) => Expression.Or(accumulate, equal)));
var p = Expression.Parameter(typeof(TElement));
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
复制代码 代码如下:
private static void QueryProducts(IQueryable<Product> query)
{
var productNames = new string[] {"P1", "P2"};
var query1 = from a in query.Where(BuildContainsExpression<Product, string>(d=>d.ProductName, productNames))
select a;
var items2 = query1.ToList();
}
private static void QueryProducts(IQueryable<Product> query)
{
var productNames = new string[] {"P1", "P2"};
var query1 = from a in query.Where(BuildContainsExpression<Product, string>(d=>d.ProductName, productNames))
select a;
var items2 = query1.ToList();
}
复制代码 代码如下:
public static IQueryable<TElement> WhereOrLike<TElement, TValue>(this IQueryable<TElement> query,
Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values)
{
return query.Where(BuildContainsExpression<TElement, TValue>(valueSelector, values));
}
private static void QueryProducts2(IQueryable<Product> query)
{
var productNames = new string[] {"P1", "P2"};
query.WhereOrLike(d=>d.ProductName, productNames).ToList();
}
复制代码 代码如下:
-- Region Parameters
DECLARE @p0 NVarChar(3) = 'P1%'
DECLARE @p1 NVarChar(3) = 'P2%'
-- EndRegion
SELECT [t0].[Id], [t0].[ProductName]
FROM [Product] AS [t0]
WHERE ([t0].[ProductName] LIKE @p0) OR ([t0].[ProductName] LIKE @p1)
新闻热点
疑难解答
图片精选