Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.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/7/sql-server/25.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
C# 实体框架中的大表_C#_Sql Server_Azure - Fatal编程技术网

C# 实体框架中的大表

C# 实体框架中的大表,c#,sql-server,azure,C#,Sql Server,Azure,我在Windows Azure Server中有一个WEB SQL,我需要在一个包含40000行的表中搜索一个项目。查询的执行时间为一分钟,对于web应用程序(或任何类型的应用程序…)来说太长了。ai这次做了什么 我的问题与此类似,但答案是不可接受的,因为分页方法太大了 带搜索的代码: public ActionResult SearchNcm(string typeSearch, string searchString) { var ncms = reposit

我在Windows Azure Server中有一个WEB SQL,我需要在一个包含40000行的表中搜索一个项目。查询的执行时间为一分钟,对于web应用程序(或任何类型的应用程序…)来说太长了。ai这次做了什么

我的问题与此类似,但答案是不可接受的,因为分页方法太大了

带搜索的代码:

    public ActionResult SearchNcm(string typeSearch, string searchString)
    {
        var ncms = repository.VIEWNCM.ToList();

        if (Request.IsAjaxRequest())
        {
            if (!String.IsNullOrEmpty(searchString))
            {
                switch (typeSearch)
                {
                    case "cod":
                        ncms = ncms.Where(e => e.CODIGO_LEITURA.ToLower().Contains(searchString.ToLower()) || e.CODIGO.ToLower().Contains(searchString.ToLower())).ToList();
                        break;
                    default:
                        ncms = ncms.Where(e => e.DESCRICAO.ToLower().Contains(searchString.ToLower())).ToList();
                        break;
                }
            }
        }



        return PartialView("BuscarNcm", ncms);
    }

不是答案,但我需要空间来扩展我的上述评论:

请记住,在迭代或调用ToList()之前,IQueryable和IEnumerable不会执行任何操作。这意味着你可以做以下事情:

var ncms = repository.VIEWNCM; // this should be IQueryable or IEnumerable - no query yet

if(Request.IsAjaxRequest())
{
    if(!string.IsNullOrEmpty(searchString))
    {
        switch(typeSearch)
        {
                case "cod":
                    // No query here either!
                    ncms = ncms.Where(e => e.CODIGO_LEITURA.ToLower().Contains(searchString.ToLower()) || e.CODIGO.ToLower().Contains(searchString.ToLower()));
                    break;
                default:
                    // Nor here!
                    ncms = ncms.Where(e => e.DESCRICAO.ToLower().Contains(searchString.ToLower()));
                    break;
            }
        }
    }
}
// This is the important bit - what happens if the request is not an AJAX request?
else
{
    ncms = ncms.Take(1000); // eg, limit to first 1000 rows
}

return PartialView("BuscarNcm", ncms.ToList()); // finally here we execute the query before going to the View

如果搜索字符串为空,您可能还需要一个默认筛选器

您想要40k行做什么?你真的需要在内存中加载它们吗?对不起,我更新了问题。我真的不需要内存中有40000行,但是在这个表上搜索非常耗时。发布LINQ查询和其他相关代码。repository.VIEWNCM.ToList()会将所有40000行返回内存吗?看起来您正在加载整个表,然后在内存中进行筛选/搜索。顺便说一下,任何涉及包含的搜索都会很慢。您可能想查看Lucene.NET以进行文本搜索(因为我相信Azure不支持全文索引)。这回答了我的问题。