LINQ multiple where statement mvc

时间:2015-10-05 18:52:27

标签: asp.net-mvc linq function

此代码仅在我填写3个字段时才有效: 网站,作者和内容。 我想完成1个字段,2个字段或3个字段(未完成的字段为空)并根据

进行搜索
var posts = from p in db.Posts
            where ((p.PostDate >= fd && p.PostDate <= td)
                && p.WebSite.Equals(WebSite)
                && p.PostAuthor.Contains(Author)
                && p.PostText.Contains(Content)
                || WebSite == null || Author == null || Content == null)
            select p;

我尝试了这段代码,但也没有用过:

var posts = from p in db.Posts
            select p;

if (WebSite != null)
{
    posts = from p in db.Posts
            where p.WebSite.Equals(WebSite)
            select p;
}

if (Author != null)
{
    posts = from p in db.Posts
            where p.PostAuthor.Contains(Author)
            select p;
}

if (Content != null)
{
    posts = from p in db.Posts
            where p.PostText.Contains(Content)
            //Count and date is missing                         
            select p;
}

return View("Index", posts);

对于1个参数我需要这样,但对于3:

var posts = from p in db.Posts
            where p.PostTitle.Contains(searchTerm) || searchTerm==null
            select p;
return View(posts);

5 个答案:

答案 0 :(得分:1)

将您的代码更改为:

var posts = from p in db.Posts
            select p;

if (!string.IsNullOrEmpty(WebSite))
{
    posts = from p in posts 
            where p.WebSite.Equals(WebSite)
            select p;
}

if (!string.IsNullOrEmpty(Author))
{
    posts = from p in posts 
            where p.PostAuthor.Contains(Author)
            select p;
}

if (!string.IsNullOrEmpty(Content))
{
    posts = from p in posts 
            where p.PostText.Contains(Content)
            //Count and date is missing                         
            select p;
}
return View("Index", posts.ToList());

空字符串与空字符串不同。

编辑:您应该在将查询发送到视图之前执行查询。

使用LINQ to Entities:

var posts = db.Posts.AsQueryable();

if (!string.IsNullOrEmpty(WebSite))
    posts = posts.Where(p => p.WebSite.Equals(WebSite));

if (!string.IsNullOrEmpty(Author))
    posts = posts.Where(p => p.PostAuthor.Contains(Author));

if (!string.IsNullOrEmpty(Content))
    posts = posts.Where(p => p.PostText.Contains(Content));

return View("Index", posts.ToList());

答案 1 :(得分:1)

首先,您需要准备未过滤的IQueryable对象而不选择输出:

IQueryable<Post> query = from p in posts;

然后根据您的过滤条件应用过滤器

if (webSite != null)
{
    posts = posts.Where(p => p.WebSite.Contains(webSite));
}
if (author != null)
{
    posts = posts.Where(p => p.PostAuthor.Contains(author));
}

Finnaly运行查询

var result = posts.ToArray();

另一种方法是在表上创建全文索引,并在索引列中按全文搜索。它比分别在三个领域中搜索有更好的结果。

答案 2 :(得分:0)

尝试以下方法:

var posts = from p in db.Posts
                where ((p.PostDate >= fd && p.PostDate <= td)
                       && ((string.IsNullOrEmpty(WebSite) || p.WebSite.Equals(WebSite))
                       || (string.IsNullOrEmpty(Author) || p.PostAuthor.Contains(Author))
                       || (string.IsNullOrEmpty(Content) || p.PostText.Contains(Content)))
                       )
                select p;

答案 3 :(得分:0)

只需将db.Posts替换为三个条件搜索中的帖子,因此对于第一个:

 if (WebSite != null)
        {
            posts = from p in posts
                    where p.WebSite.Equals(WebSite)
                    select p;
        }

答案 4 :(得分:0)

解决方案:

var posts = from p in db.Posts
                        where ((string.IsNullOrEmpty(WebSite) || p.WebSite.Equals(WebSite))
                               && (string.IsNullOrEmpty(Author) || p.PostAuthor.Contains(Author))
                               && (string.IsNullOrEmpty(Content) || p.PostText.Contains(Content))
                               )
                        select p;