0

I'm using the zip function to merge two IEnumerable objects together in a C# app in Visual Studio. I have the following code:

static void Main(){

    using (var conn = new SqlConnection(connectionString)){
          conn.Open();

          IEnumerable<string> tmp1 = conn.Query<string>("SELECT NAME FROM Table1");
          IEnumerable<string> tmp2 = conn.Query<string>("SELECT Type FROM Table1");
          IEnumerable<decimal> mock1 = conn.Query<decimal>("SELECT value1 FROM mockData1");
          IEnumerable<decimal> mock2 = conn.Query<decimal>("select value2 FROM mockData2");

          foreach (var item in tmp1.Zip(tmp2, (a, b) => new { a, b })){
             Console.WriteLine(item.a);
             Console.WriteLine(item.b);
             Console.ReadKey();
          }
    }

}

When I run the code above, the following is output by the console:

CorrectName1
CorrectType1
CorrectName2
CorrectType2
....

which is what I expected. However, when I make the following changes:

foreach (var item in mock1.Zip(mock2, (a, b) => new { a, b })){
        Console.WriteLine(item.a);
        Console.WriteLine(item.b);
        Console.ReadKey();
}

the program skips the console output completely and there are no errors in the error list.

Essentially, why would the program work as expected for IEnumerables of type string and not for IEnumerables of type decimal? Before you ask, I have checked both of the queries through Management Studio and they are correct, and I'm able to iterate over mock1 and mock2 in foreach loops that involve just the two collections independently, but for some reason it seems that the zip function fails for IEnumerable< decimal >.

EDIT: Here are two foreach loops to confirm my hypothesis that the queries are not bad:

foreach (var item in mock1){
    Console.WriteLine(item);
    Console.ReadKey();
}

Output demo for mock1:

Value from mock1 (1)
Value from mock1 (2)
...

/***/

foreach (var item in mock2){
    Console.WriteLine(item);
    Console.ReadKey();
}

Output demo for mock2:

Value from mock2 (1)
Value from mock2 (2)
...
6
  • I'm not seeing any changes..
    – Orel Eraki
    Jun 27, 2016 at 21:49
  • My bad, how about now? Jun 27, 2016 at 21:52
  • 2
    Are you sure rows are returned? if you do Console.WriteLine(mock1.Count());Console.WriteLine(mock2.Count()); do you get 0, 0? Jun 27, 2016 at 21:54
  • There's no reason why Zip wouldn't work on collections of decimals. So either your data is bad (mock1 and/or mock2 is empty) or something else is going on that's not represented in your sample code.
    – D Stanley
    Jun 27, 2016 at 21:57
  • 1
    @Scott Chamberlain that was correct, mockData1 had 20 and mockData2 had 0 Jun 27, 2016 at 22:07

2 Answers 2

2

My guess is your database is not returning data for the queries. It works fine when replacing the query with arrays:

void Main()
{
    IEnumerable<decimal> mock1 = new decimal[] { 1, 2, 3 };
    IEnumerable<decimal> mock2 = new decimal[] { 4, 5, 6 };

    foreach (var item in mock1.Zip(mock2, (a, b) => new { a, b }))
    {
        Console.WriteLine(item.a);
        Console.WriteLine(item.b);
        Console.Read();
    }
}

1

4

2

5

3

6

3
  • The program should give different output that what you've posted.
    – D Stanley
    Jun 27, 2016 at 21:58
  • In the last sentence of my question I mentioned that I already tested the queries, but just to be absolutely explicit I'll re-edit the question to include the tested code. Jun 27, 2016 at 21:58
  • 1
    Try calling ToList or AsEnumerable on the Query method result if it returns IQueryable<T>. It could be that if there is a query provider it is getting confused by Zip.
    – codekaizen
    Jun 27, 2016 at 22:02
0

It was my fault - I had ten data points for both mockData1 and mockData2 but during my manual SQL insertions I accidentally added all 20 to mockData1 and none to mockData2.

Not the answer you're looking for? Browse other questions tagged or ask your own question.