I am trying to convert the above statement to LINQ. I have multiple tables (with an identifier in the first column) and I need to "multiply" them so that I get a table as a result where all possible combinations are shown (e.g. table 1 = 2 rows, table 2 = 2 rows, table 3 = 2 rows, resulting table 2*2*2 = 8 rows).
If I have a sqlite db I used to do it in this way:
string sql = "SELECT * FROM tab1,tab2,tab3;
SQLDataAdapter adapter = new SQLiteDataAdapter(sql, con);
adapter.Fill(ds.Tables[1]);
Now the db is not longer a sqlite db but a dataset. This dataset contains the tab1,tab2,tab3 etc. Now I would like to do the same as before - multiply the tables. How can this be done with a dataset (by using LINQ if possible)?
In a LINQ query expression you just need multiple from
clauses, e.g.
var query = from row1 in tab1
from row2 in tab2
from row3 in tab3
select new { row1, row2, row3 };
If you don't want to use query expressions, you'd use SelectMany
instead, but in this case a query expression is going to be simpler.
For datasets, you'd probably want something like:
var query = from row1 in ds.Tables["tab1"].AsEnumerable()
from row2 in ds.Tables["tab2"].AsEnumerable()
from row3 in ds.Tables["tab3"].AsEnumerable()
select new { row1, row2, row3 };
Make sure you have a using
directive for System.Data
so that you can pull in DataTableExtensions
for AsEnumerable
.
See more on this question at Stackoverflow