LINQ是否可以动态添加where子句 [英] LINQ is it possible to add where clauses dynamically

查看:158
本文介绍了LINQ是否可以动态添加where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用不同的键搜索数据库.根据输入,可能有1键到10键.有没有一种方法可以动态地将OR/AND子句添加到我的Linq查询中?

I want to search my db with different keys. According to the input, there may be 1 key to 10 keys. Is there a way to add OR/AND clauses to my Linq query dynamically?

 keys[k] // I have my keys in this array 
 var feedList = (from feed in ctx.Feed
                 where feed.content.contains(keys[0]) 
                       && feed.content.contains(keys[1])
                       && ... // continues with the keys.length
                 select new {
                    FeedId = feed.DuyuruId,
                    FeedTitle = feed.FeedTitle,
                    FeedContent = feed.FeedContents,
                    FeedAuthor = user.UserName + " " +User.UserSurname
 }

推荐答案

您可以尝试

You could try an .All clause to check all the keys:

where keys.All(key => feed.content.contains(key))

这篇关于LINQ是否可以动态添加where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆