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.
I guess something like this should work for you:
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
d.Field5, d.Field6})
}).ToList();
var result = queryResult.Select(r => new Storage
{
myField1 = r.Field1,
myField2 = r.Field2,
myField3 = r.Field3,
myField4 = r.myField4.SelectMany(t => new []
{
Tuple<int,int>(t.Field1, t.Field2),
Tuple<int,int>(t.Field3, t.Field4),
Tuple<int,int>(t.Field5, t.Field6)
}).ToList()
}).ToList();
return result;
Alternative version with double 'foreach' loop should work in the same way but it will not create redundant arrays of tuples:
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
d.Field5, d.Field6})
}).ToList();
var result = new List<Storage>();
foreach(var row in queryResult){
var storage = new Strorage
{
myField1 = r.Field1,
myField2 = r.Field2,
myField3 = r.Field3,
myField4 = new List<Tuple<int,int>>()
};
foreach(var subRow in row.myField4)
{
storage.myField4.Add(Tuple<int,int>(t.Field1, t.Field2));
storage.myField4.Add(Tuple<int,int>(t.Field3, t.Field4));
storage.myField4.Add(Tuple<int,int>(t.Field5, t.Field6));
}
}
return result;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.