I need to fill a Tuple-List with values from a MSSQL-Database with help of Linq to Entities. The code snippet below will help to get a Tuple-List where 1 Database row reflects 1 Tuple Entry. In case there are 3 rows for calculateData we would get 3 Tuple Entries in that list created with Field1
to Field4
.
The code below makes it possible:
var queryResult = (from a in calculate
join b in calculateData on a.Id equals b.CalcId into c
where a.SpecialID == 2023 && a.VersionId == 1
orderby a.InternalOrderNr ascending
select new
{
a.Field1,
a.Field2,
a.Field3,
myField4 = c.Select(d => new {
d.Field1, d.Field2, d.Field3, d.Field4})
}).ToList();
var result = queryResult.Select(r => new Storage
{
myField1 = r.Field1,
myField2 = r.Field2,
myField3 = r.Field3,
myField4 = r.myField4.Select(t => new Tuple<int,int,decimal,string>
(
t.Field1,
t.Field2,
t.Field3,
t.Field4)
).ToList()
});
return result;
But what I need is a bit different. I need for those 3 database rows in total 9 Tuple Entries in the list. Example:
Database contains 4 Columns (Column1, Column2, Column3, Column4) per row.
And we have 3 Rows.
Tuple 1: DB-Row1 and Column1, Column2, 0, 0
Tuple 2: DB-Row1 and Columm2, Column3 ,0 ,1
Tuple 3: DB-Row1 and Columm3, Column4 ,0 ,2
Tuple 4: DB-Row2 and Column1, Column2, 1, 0
Tuple 5: DB-Row2 and Columm2, Column3 ,1 ,1
Tuple 6: DB-Row2 and Columm3, Column4 ,1 ,2
Tuple 7: DB-Row3 and Column1, Column2, 2, 0
Tuple 8: DB-Row3 and Columm2, Column3 ,2 ,1
Tuple 9: DB-Row3 and Columm3, Column4 ,2 ,2
So the difference here is that there is not a 1:1 Mapping between database row and Tuple. I need to make several Tuple out of 1 Database row. In the example above we have 3 Tuple entries, but it can differ and can be more or less.
Tuple 1: DB-Row1 and Column1, Column2, 0, 0
? And did you mean by col1, col2, col0(see previous question), col0 of the first row? – Xiaoy312