Linq to sql add / dynamic OR WHERE子句

时间:2014-04-16 17:03:15

标签: c# sql sql-server linq

有主要查询

选择项目FROM QuickRecords WHERE(CategoryId = @CategoryId AND CategoryUserId = @UserId)

            var q = from items in db.QuickRecords
               where items.CategoryId == category && items.CategoryUserId == UserId
               select items;

有动态过滤器,样本“22,333”

            string[] filters = filter.Split(',');
            foreach (string item in filters)
            {
                string f = "%" + item.ToLower() + "%";
                q = q.Where(x => x.Record.Contains(f));
            }

如何添加到SQL查询

AND(记录喜欢'%22%'或记录喜欢'%333%')

1 个答案:

答案 0 :(得分:2)

string[] filters = filter.Split(',');
var q = from items in db.QuickRecords
               where items.CategoryId == category && items.CategoryUserId == UserId
                      && filters.Any(f=> item.Record.Contains(f))
               select items;