Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.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#_Asp.net_Sql_Linq - Fatal编程技术网

C# 如何查询大表和列表对象

C# 如何查询大表和列表对象,c#,asp.net,sql,linq,C#,Asp.net,Sql,Linq,我有一个大约120万行的大表,需要使用它进行查询。在同一字段中包含。需要使用列表查询字段组合名称。我现在只尝试使用一个字段来检查性能,这反映在代码中。我写它的方式花了太长时间。有没有一种方法不必将表加载到内存中 JDataClassDataContext db = new JDataClassDataContext(); var fullName = txtSearchBox0.Text.Trim(); List<string> firstName = new List<str

我有一个大约120万行的大表,需要使用它进行查询。在同一字段中包含。需要使用列表查询字段组合名称。我现在只尝试使用一个字段来检查性能,这反映在代码中。我写它的方式花了太长时间。有没有一种方法不必将表加载到内存中

JDataClassDataContext db = new JDataClassDataContext();
var fullName = txtSearchBox0.Text.Trim();
List<string> firstName = new List<string>(txtSearchBox1.Text.Split(',').Select(x => Convert.ToString(x)).ToList());

var rows = (from c in db.defendants_ALLs.AsEnumerable()
      where c.combined_name.Contains(fullName)
          && firstName.Any(n => c.combined_name.Contains(n))
      select c).ToList();     

dlSearch.DataSource = rows;
dlSearch.DataBind();
JDataClassDataContext db=new JDataClassDataContext();
var fullName=txtSearchBox0.Text.Trim();
List firstName=新列表(txtSearchBox1.Text.Split(',')。选择(x=>Convert.ToString(x)).ToList());
var rows=(从数据库中的c开始。被告人_all.AsEnumerable()
其中c.combined_name.Contains(全名)
&&firstName.Any(n=>c.combined_name.Contains(n))
选择c.ToList();
dlSearch.DataSource=行;
dlSearch.DataBind();
签出和方法

您可以这样使用它:

var rows = (from c in db.defendants_ALLs.AsEnumerable()
           where c.combined_name.Contains(fullName)
           && firstName.Any(n => c.combined_name.Contains(n))
           select c).OrderBy(o => o.id).Skip(amount).Take(otherAmount).ToList();

其中,
amount
otherAmount
表示实际从数据库中获取的记录数量。这意味着您只能为Instance请求30条记录,而不是全部120万条(=如果您只需执行
.ToList()

最好的方法是生成一个好的oldfasion SQL查询,如 “从表中选择字段,其中包含组合的名称,如“%fullname%”和firstName('first'、'next'、…)”
您应该了解SqlConnection、SqlCommand和SqlDataReader。它与linq稍有不同,但速度要快得多(如果您有正确的索引)

您不能在数据库表中使用fullname作为字段吗?在您当前的设置中,rdbms不可能使用索引。它返回到完整表扫描,因此performance hit.fullname是网页上的一个字段,需要用户输入,因此它不能在数据库中。我必须输入字段txtSearch0和txtSearch1,这两个字段都需要用户输入来搜索组合的_名称以查找正确的名称。例如,txtSearch0='smit'和txtSearch1='joh,jon,j'。以获得所有不同的拼写和随机j。史密斯,这正好在我们得到的数据中。我认为存储过程可能是目前最好的选择。使用skip()和take()会得到所有正确的记录吗?问题是该字段可以包含诸如“smith john”、“smith j.”、“john smith”、“john d”等字符串。史密斯博士。这取决于你所说的“正确记录”。使用skip&take可能意味着您在一个名称包含“john”的地方获取40条记录,但实际上可能有200多条记录包含“john”的名称。这些其他记录将不会从数据库中提取。“正确的记录”将是包括“jon,joh,j.”和“smit”的所有记录。由于人为输入错误,我们不得不手工缩小这些记录的范围。使用存储过程会更快吗?索引是正确的,我们有一个访问应用程序,已经执行相同的功能,但我们想摆脱自己的访问。