Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc LINQ多where语句mvc_Asp.net Mvc_Linq_Function - Fatal编程技术网

Asp.net mvc LINQ多where语句mvc

Asp.net mvc LINQ多where语句mvc,asp.net-mvc,linq,function,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) &&

此代码仅在我完成以下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;
对于1个参数,但对于3个参数,我需要这样:

var posts = from p in db.Posts
            where p.PostTitle.Contains(searchTerm) || searchTerm==null
            select p;
return View(posts);
请尝试以下操作:

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;

var posts=从p到db.posts
其中((p.PostDate>=fd&&p.PostDate请尝试以下操作:

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;

var posts=从p到db.posts

其中((p.PostDate>=fd&&p.PostDate只需在三个条件搜索中将db.Posts替换为Posts,因此对于第一个条件搜索:

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

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

 if (WebSite != null)
        {
            posts = from p in posts
                    where p.WebSite.Equals(WebSite)
                    select p;
        }
解决方案:

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;
解决方案:

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;

将代码更改为:

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创建实体:

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());

将代码更改为:

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创建实体:

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());

首先,您需要在不选择输出的情况下准备非过滤IQueryable对象:

IQueryable<Post> query = from p in posts;
最终运行查询

var result = posts.ToArray();

另一种方法是在表上创建全文索引,并在索引列中按全文进行搜索。与在三个字段中分别搜索相比,它具有更好的结果。

首先,您需要准备未筛选的IQueryable对象,而无需选择输出:

IQueryable<Post> query = from p in posts;
最终运行查询

var result = posts.ToArray();


另一种方法是在表上创建全文索引,并在索引列中按全文搜索。它比在三个字段中分别搜索效果更好。

不起作用,此方法单独搜索任何参数,而不是一起搜索,如果全部为空,则不显示所有列表@yohanis@yohanis我想你只是想移除否定(
)构成每个空/空检查。我已编辑了上面的答案。看起来您需要在三者之间进行OR运算。请尝试编辑的一个。此外,我们不需要@DStanley建议的否定。@yohanis我理解您的逻辑,将所有字段一起查找,而不是分开查找:(var posts=数据库中的p,其中((string.IsNullOrEmpty(网站)| | p.WebSite.Equals(网站))&&&(string.IsNullOrEmpty(作者)| p.PostAuthor.Contains(作者))&(string.IsNullOrEmpty(内容)| p.PostText.Contains(内容)))选择p;不起作用,这将单独搜索任何参数,而不是一起搜索,如果全部为空,则不会显示所有列表@yohanis@yohanis我想您只是想删除否定(
)形成每个空/空检查。我已经编辑了上面的答案。看起来您需要在这三个答案之间进行OR运算。请尝试编辑后的一个。另外,我们不需要@DStanley建议的否定。@yohanis我理解您的逻辑,将所有字段一起查找,而不是分开查找:(var posts=来自p in db.posts where((string.IsNullOrEmpty(网站)| p.WebSite.Equals(网站))&&(string.IsNullOrEmpty(作者)| p.PostAuthor.Contains(作者))&&(string.IsNullOrEmpty(内容)| p.PostText.Contains(内容)))选择p;无效。同时搜索3个字段,但不能单独搜索或使用nulls@user3077648-与其说不起作用,不如试着去理解为什么它不起作用。只要看看如果你真的考虑到发生了什么,你的问题能得到多明显的回答。我知道这个查询得到了所有的答案一起搜索值,但无法理解为什么每个语句是分开的。我试图在过去4小时内修复它,但不起作用。一起搜索3个字段,但不能分开或使用nulls@user3077648-与其说不起作用,不如试着去理解它为什么不起作用。看看如果你只是采取行动,你的问题会得到多明显的回答我仔细考虑了发生的事情。我理解查询将所有值都收集在一起,但不理解为什么每个语句都是分开的。我在过去4小时内尝试修复它解释说不起作用。您是否收到错误?或者您是否没有得到正确的结果?我只有在完成3个字段后才能得到结果:网站+授权或者+内容,但是如果我只完成了其中的1或2篇,我会得到所有的帖子,如果我把所有的帖子都留为空。所有的帖子都没有错误@JB06Ok。如果你把它们都留为空,那么返回所有的帖子是正确的行为。所以这种情况是正确的。你使用的任何原因都是。等于而不是==?解释不起作用。你有错误吗?或者你没有得到答案是否输入正确的结果?只有当我填写了3个字段:网站+作者+内容,我才会得到结果,但如果我只填写了其中的1或2个字段,我会得到所有帖子,如果我将所有帖子都留空,那么所有帖子都是空的。@JB06Ok没有错误。如果您将它们全部留空,那么返回所有帖子是正确的行为。因此,这种情况是正确的。您使用的任何原因.Equals而不是==?比尝试在where子句中单列一行要好得多,就像这里的其他答案一样。谢谢。我可以使用流畅的LINQ来清理它,但不确定OP是否想使用它。是的,就我个人而言,LINQ to SQL让我毛骨悚然。我希望每个人都能放下它,使用LINQ to Entities。更干净的版本同样适用于LINQ over SQL。尽管如此,我相信您在执行
var posts=db.posts;
时会遇到问题,因为它返回一个
DbSet
,其余的查询将返回
IQueryable
。一个简单的解决方案是执行
var posts=db.posts.AsQueryable()
@RobertMcKee很好。我用你的建议更新了答案。比像答案的其余部分那样在where子句中单列一行要好得多