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
C# 如何从文本框C获取LINQ中的输入_C#_Linq - Fatal编程技术网

C# 如何从文本框C获取LINQ中的输入

C# 如何从文本框C获取LINQ中的输入,c#,linq,C#,Linq,我有一个数据表dtu Customers,其中包含一些客户信息。在这个数据表中,我想选择一系列的邮政编码,用户将使用文本框输入这些邮政编码 我正在使用以下代码: IEnumerable<DataRow> enumerableDataRowCollection = from company in dt_Customers.AsEnumerable() let zip = company.Field<string>("ZIP") where (!string

我有一个数据表dtu Customers,其中包含一些客户信息。在这个数据表中,我想选择一系列的邮政编码,用户将使用文本框输入这些邮政编码

我正在使用以下代码:

IEnumerable<DataRow> enumerableDataRowCollection =
   from company in dt_Customers.AsEnumerable()
   let zip = company.Field<string>("ZIP")
   where (!string.IsNullOrEmpty(zip) && (zip[0] >= "'" + txtBox_ZIP_From.Text + "'" && zip[0] <=  "'" + txtBox_ZIP_to.Text + "'"))

   select company;
当我硬编码一些值时,上面的代码工作得很好,如下所示:

zip[0] >= '2' && zip[0] <= '6'
Zip[0]是一个字符,txtBox\u Zip\u From.Text是一个字符串。在给定的硬编码示例中,您正在比较一个字符和一个字符

IEnumerable<DataRow> enumerableDataRowCollection =
   from company in dt_Customers.AsEnumerable()
   let zip = company.Field<string>("ZIP")
   where (!string.IsNullOrEmpty(zip) && (zip >= txtBox_ZIP_From.Text && zip <=  txtBox_ZIP_to.Text))
如果txtBox包含单个字符

var cCriterFrom = txtBox_ZIP_From.Text.Text[0];
var cCriterTo = txtBox_ZIP_to.Text.Text[0];

IEnumerable<DataRow> enumerableDataRowCollection =
   from company in dt_Customers.AsEnumerable()
   let zip = company.Field<string>("ZIP")
   where (!string.IsNullOrEmpty(zip) && (zip[0] >= cCriterFrom && zip[0] <= cCriterTo))
问题是zip是一个字符串,因此zip[n]是一个字符。如果要比较字符串,请尝试以下操作:

string.Compare(zip, txtBox_ZIP_From.Text) >= 0 &&
string.Compare(zip, txtBox_ZIP_To.Text) <= 0
试试这个:

IEnumerable<DataRow> enumerableDataRowCollection =
   from company in dt_Customers.AsEnumerable()
   let zip = company.Field<string>("ZIP")
   where (!string.IsNullOrEmpty(zip) && (zip.CompareTo(txtBox_ZIP_From.Text) >= 0) && (zip.CompareTo(txtBox_ZIP_to.Text) <=0))    
   select company;

我想从邮政编码中选择第一个数字,这就是我使用char数据类型和我使用ZIP[0]的原因。@Kami您为什么要使用“+txtBox\u ZIP\u from.Text+”?ZIP[0]>=cCriter&&ZIP[0]
zip[0] >= txtBox_ZIP_From.Text[0] && zip[0] <=  txtBox_ZIP_to.Text[0]
IEnumerable<DataRow> enumerableDataRowCollection =
   from company in dt_Customers.AsEnumerable()
   let zip = company.Field<string>("ZIP")
   where (!string.IsNullOrEmpty(zip) && (zip.CompareTo(txtBox_ZIP_From.Text) >= 0) && (zip.CompareTo(txtBox_ZIP_to.Text) <=0))    
   select company;