3

I am trying to create a query. Here is the code

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

I am using for loop to create a IN query. Is it possible via LINQ?

Output should be

'0000000087','0000000088','0000000089'

Second thing, I get the code from a textbox value like 0000000087. When I convert it into int using Convert.ToInt32, preceding 0s get disappeared.

Is it possible that 0s shouldn't get disappear since number of preceding 0s may vary?

Harshit
  • 5,147
  • 9
  • 46
  • 93
  • Can you please tell what you are passing into CodeTextBox and into CountTextBox? EDIT: Too early in the morning. Now i got it... – etalon11 Jan 25 '16 at 06:38
  • Regarding the leading 0's; The integer 0001 is identical to 0000001 is identical to 1. – Andy Lamb Jan 25 '16 at 06:38
  • 1
    Can't understand your problem? Is it query string for SqlCommand? What is your actual problem (exception or unexpected result)? Where is your query parameter (add more code of creating full query string)? – Vadim Martynov Jan 25 '16 at 06:38
  • @etalon11, I am passing `0000000087` & `5` in textboxes respectively – Harshit Jan 25 '16 at 06:39
  • your code seems to have problem.... in for loop, int I (capital letter) is changed to i++ (small letter)...? – JGV Jan 25 '16 at 06:39
  • @AndyLamb, I know but I want to use it as string in sql query to match the values – Harshit Jan 25 '16 at 06:40
  • @VadimMartynov, I want to replace the for loop to LINQ. Also I want to prevent 0s to get disappeared. – Harshit Jan 25 '16 at 06:44
  • Where does `fromcode` come from? – Kosala W Jan 25 '16 at 06:45
  • @KosalaW, sorry, forgot to write the same in variable initialization! – Harshit Jan 25 '16 at 06:46
  • 1
    @HarshitShrivastava Try following. `var whereQueryFormat = "Whre columnName IN ({0})"; var wherequery = string.Join(",", Enumerable.Range(1, count).Select(i => "'" + fromcode + i + "'")); wherequery = string.Format(whereQueryFormat, wherequery);` – tchelidze Jan 25 '16 at 06:49
  • AFAIK, linq does not support IN operator, but you can emulate it. see http://stackoverflow.com/questions/2334327/what-is-the-linq-equivalent-to-the-sql-in-operator. Using something like NPOCO you can use IN operator with array parameter which will be translated to array of parameters (has some advantages). In fact, if you have a lot of codes for your IN operator and you want to be optimal - use table value parameter and stored proc (and, by the way, replace iterative string concat with string builder or string[].Join). – SalientBrain Jan 25 '16 at 06:50
  • You can't use LINQ instead of this loop. – Vadim Martynov Jan 25 '16 at 06:51

5 Answers5

1

I am not quite sure if you can use LINQ at this point. Here is an example how I would solve this problem:

   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, "','") & "')"

Anyway: Why is your database-field a string and not a integer? Then you would not have a problem with the leading zeros.

etalon11
  • 895
  • 2
  • 13
  • 36
1

No need for .PadLeft(8,'0') which causes unnecessary leading zeros. Try following.

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

If you want to remove the for operation, you could probably do it this way using LINQ, Enumerable.Range, and 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);
Ian
  • 30,182
  • 19
  • 69
  • 107
1

If you want to use LINQ to get the IN clause you can use this:

var range = Enumerable.Range(0, count).Select(x => 
            string.Format("'{0}'", x.ToString("0000000000")));
var inCluase = string.Format(" IN ({0})", string.Join(",", range));
jvanrhyn
  • 2,804
  • 19
  • 14
1

You will still have to use a loop for this. But the below code address your variable padded zeros issue.

       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);
Kosala W
  • 2,133
  • 1
  • 15
  • 20