linq where 与 或

List<Product> GetProductsByOR(params string[] keywords)
{
DBDataContext db = new DBDataContext(Database.ConnectionString);
var predicate = PredicateBuilder.False<Product>();
foreach (string keyword in keywords)
{
string temp = keyword;
predicate = predicate.Or(p => p.Description.Contains(temp));
}
var query = db.Products.Where(predicate);
//翻译后的sql语句:
//Select [t0].[ID], [t0].[Name], [t0].[Description]
//FROM [dbo].[Product] AS [t0]
//Where ([t0].[Description] LIKE '%6111%') OR ([t0].[Description] LIKE '%2350%')
return query.ToList();
}

上一篇:lodash.js 学习记录


下一篇:码农行业的术语,为啥又多,又难懂?