Sample table 'table':

| table1 ---- | ---- table2 | ---- table3 ---- |
| ------------ | ------------ | ---------------- |
| User ------ | 5000 ----- | -15-02-2016 |
| admin ---- | 3500 ----- | -18-03-2016 |
| guest ----- | 2000 ----- | -15-02-2016 |

I make a request:

SELECT * FROM table WHERE CONVERT(VARCHAR(10),table3,102) LIKE CONVERT(VARCHAR(10),15-02-2016,102) 

The request gives me:

 |User------| 5000-----|-15-02-2016| |guest-----| 2000-----|-15-02-2016| 

In fact, the query gives me a lot more rows and I need to loop through each row of this query and work on the data obtained. For ease of understanding, I'll write a little clearer

  for (int i = 0; i <= _selectedRowCount- 1; i++) { string s= //тут данный с столбца table1 i строки запроса int ammount=//тут данный с столбца table2 i строки запроса // //... работа с полученными данными // } 

How can you realize something like this? In general, you need to go through each line of the request in turn. The processed data is transmitted to an external source. More precisely in Excel.

    2 answers 2

    Let query result of your query, then:

     var result = query.Select(element => new { s = element[table1], amount = element[table2]}); 

    result already being transmitted to an external source.

      I solved the problem on my own cases like this:

       WITH MyTable AS ( SELECT ROW_NUMBER() OVER (ORDER BY table1) AS RowNum, * FROM table WHERE CONVERT(VARCHAR(10),table3,102) LIKE CONVERT(VARCHAR(10),15-02-2016,102) ) SELECT * FROM MyTable WHERE MyTable .RowNum=2 

      I execute this query inside the loop but for the place

        RowNum=2 

      put

        RowNum=i 

      and I get each line of the request one at a time and work as I wanted. Maybe someone will help my way. If there is a more correct approach, I would like to see.

      • one
        Power on your server, most likely. It is better to take the data in one request and then process it on the client. - Mirdin
      • You're right. Then you have to think where to pick up and how to process it. It is necessary to take C # - Dowlpack
      • one
        Let's begin, here with what that you use, for connection with a database (DataReader, DataSet, EF, LinqToSQL ...). When you have a certain set of data on hand, it will already be easy to process it with Linq, and write down where you write there. - Mirdin