tags:

views:

211

answers:

3

I want to take a table as represented by a multidimensional string array (column names in another array) and use a SQL SELECT statement to get a subset of rows.

The Catch: - the table is input by the user (different table each time) - the SQL is also input by the user

Do I need to: 1) Create a table in SQL 2) Populate the table in SQL 3) Query the table

or is the a simpler solution? E.g. converting the multidimensional array to a DataTable and then running the SQL on that object?

A: 

Simplest way is to use LINQ

Chris
Can LINQ be input as a string by the user?
Chad
+3  A: 

I think you would be able to use DataTable for this. It's normally used to store data retrieved from a database but you can populate it manually. The best part is the DataTable.Select() method, which allows you to write just the WHERE clause of a query and it will return the matching rows.

Telos
That is the direction I was leaning (in combination with Starsignleo's comment). I was thinking of parsing the query into the various components (SELECT, WHERE, ORDER, GROUP).<br />Upon further research: Dynamic LINQ might be a better possibility
Chad
+1  A: 

You can create your own expression tree representing the query the user enters. This is how Linq works, under the hood. If you could give an example of what your trying to achieve it may help also what your going to write the application in c# for web for example.

For instance if you letting your users enter new rows, in some kind of GUI to a table then you could, do this in a datagrid and enable column filter to achieve the result mentioned above?

In a web app you could have an input box above each column users can enter data to filter that column on.

StarSignLeo
The data comes in a variety of formats (e.g. CSV, Tab/Return), but they are converted into a 2D array. The minimum set of required features is: SELECT, WHERE, ORDER, and GROUP. However, I would prefer full support for all SQL queries (including JOINs). The problem is that programatically adding a table each time to perform a single query seems inefficient, complicated, and slow.
Chad
That maybe but what you want to do is a little complex. Do you really need to Group? Most of these features you can get out of the box with the Datagrid, and Devexpress Grid we use here at work also supports grouping.
StarSignLeo