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中的增量计数#_C#_Linq - Fatal编程技术网

C# c中的增量计数#

C# c中的增量计数#,c#,linq,C#,Linq,我正在尝试创建一个查询。这是密码 string wherequery = ""; int fromcode = Convert.ToInt32(CodeTextBox.Text); int count = Convert.ToInt32(CountTextBox.Text); for (int i = 0; i < count; i++) wherequery += ("'" + (fromcode + i).ToString().PadLeft(8,'0') + "',");

我正在尝试创建一个查询。这是密码

string wherequery = "";
int fromcode = Convert.ToInt32(CodeTextBox.Text);
int count = Convert.ToInt32(CountTextBox.Text);

for (int i = 0; i < count; i++)
    wherequery += ("'" + (fromcode + i).ToString().PadLeft(8,'0') + "',");
wherequery = wherequery.TrimEnd(",");
第二件事,我从文本框值中获取代码,如
0000000087
。当我使用
convert.ToInt32
将其转换为int时,前面的0将消失


由于前面0的数量可能会有所不同,所以0是否可能不会消失?

我不太确定此时是否可以使用LINQ。以下是我如何解决此问题的示例:

   Dim whereQuery As String = ""
    Dim operatorValues As New List(Of String)
    Dim startingNumber As String = CodeTextBox.Text
    Dim lengthOfNumber As Integer = startingNumber.Length
    Dim count As Integer = Convert.ToInt32(CountTextBox.text)

    For i As Integer = CInt(startingNumber) To CInt(startingNumber + count - 1)
        operatorValues.Add(i.ToString.PadLeft(lengthOfNumber, "0"))
    Next

    whereQuery &= "IN ('" & Join(operatorValues.ToArray, "','") & "')"
无论如何:为什么数据库字段是字符串而不是整数?这样,前导零就不会有问题。

不需要
.PadLeft(8,'0')
,这会导致不必要的前导零。 试试下面的

var whereQueryFormat = "Whre columnName IN ({0})"; 
var wherequery = string.Join
(",", 
  Enumerable.Range(1, count)
  .Select(i => "'" + fromcode + i + "'")
); 
wherequery = string.Format(whereQueryFormat, wherequery);

如果要删除
for
操作,可以使用
LINQ
Enumerable.Range
string.Join来执行此操作:

int fromcode = Convert.ToInt32(CodeTextBox.Text);
int count = Convert.ToInt32(CountTextBox.Text);
var words = from i in Enumerable.Range(fromcode, count)
            select "'" + i.ToString().PadLeft(8, '0') + "'";
string wherequery = string.Join(",", words);

如果要使用LINQ获取IN子句,可以使用以下命令:

var range = Enumerable.Range(0, count).Select(x => 
            string.Format("'{0}'", x.ToString("0000000000")));
var inCluase = string.Format(" IN ({0})", string.Join(",", range));

您仍然需要为此使用循环。但是下面的代码解决了变量填充零的问题

       var enteredValue = "00000088";//get this from the text box
       int enteredCount = 10;//get this from the text box

       string wherequery = "";
       int fromCode = Convert.ToInt32(enteredValue);
       string fromCodeStr = fromCode.ToString(CultureInfo.CurrentCulture);
       string padZeros = enteredValue.Split(new[] {fromCodeStr}, StringSplitOptions.None)[0];
       List<string> searchList =  new List<string>();
       for (int i = 1; i <= enteredCount; i++)
       {
           searchList.Add(padZeros + fromCode + i);
       }
       var searchString = string.Join(",", searchList);
var enteredValue=“000000 88”//从文本框中获取此信息
int enteredCount=10//从文本框中获取此信息
字符串wherequery=“”;
int fromCode=Convert.ToInt32(输入值);
string fromCodeStr=fromCode.ToString(CultureInfo.CurrentCulture);
string padZeros=enteredValue.Split(new[]{fromCodeStr},StringSplitOptions.None)[0];
列表搜索列表=新列表();

对于(int i=1;我能告诉你你正在传递什么到CodeTextBox和CountTextBox吗?编辑:早上太早了。现在我得到了它…关于前导0;整数0001和000000相同1和1相同。无法理解你的问题?是SqlCommand的查询字符串吗?你实际的问题是什么(异常或意外结果)?您的查询参数(添加更多创建完整查询字符串的代码)在哪里?@etalon11,我正在文本框中传递
0000000087
&
5
respectively@HarshitShrivastava尝试以下操作。
var whereQueryFormat=“Whre columnName IN({0})”;var wherequery=string.Join(“,”,Enumerable.Range(1,计数)。选择(i=>““+”fromcode+i+”);wherequery=string.Format(whereQueryFormat,wherequery);
VB?谁要的?@KosalaW:Nobody:D如果你看不懂,就使用在线转换器();)哈哈。哦,是的……下次我要用vb3.0写答案。听起来怎么样?
       var enteredValue = "00000088";//get this from the text box
       int enteredCount = 10;//get this from the text box

       string wherequery = "";
       int fromCode = Convert.ToInt32(enteredValue);
       string fromCodeStr = fromCode.ToString(CultureInfo.CurrentCulture);
       string padZeros = enteredValue.Split(new[] {fromCodeStr}, StringSplitOptions.None)[0];
       List<string> searchList =  new List<string>();
       for (int i = 1; i <= enteredCount; i++)
       {
           searchList.Add(padZeros + fromCode + i);
       }
       var searchString = string.Join(",", searchList);