c#LINQ查询填充元组列表

时间:2016-04-26 20:28:16

标签: c# linq tuples

我需要在Linq to Entities的帮助下用MSSQL数据库中的值填充Tuple-List。下面的代码片段将有助于获取一个Tuple-List,其中1个数据库行反映1个元组条目。如果calculateData有3行,我们将在使用Field1Field4创建的列表中获得3个元组条目。

以下代码使其成为可能:

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;

但我需要的是有点不同。我需要列表中总共9个元组条目中的3个数据库行。例如:

数据库每行包含4列(Column1,Column2,Column3,Column4) 我们有3排。

  

元组1:DB-Row1和Column1,Column2,0,0   元组2:DB-Row1和Columm2,Column3,0,1   元组3:DB-Row1和Columm3,Column4,0,2   元组4:DB-Row2和Column1,Column2,1,0   元组5:DB-Row2和Columm2,Column3,1,1   元组6:DB-Row2和Columm3,Column4,1,2   元组7:DB-Row3和Column1,Column2,2,0   元组8:DB-Row3和Columm2,Column3,2,1   元组9:DB-Row3和Columm3,Column4,2,2

所以这里的区别在于数据库行和元组之间没有1:1的映射。我需要在1个数据库行中创建几个Tuple。在上面的示例中,我们有3个元组条目,但它可以有所不同,可以或多或少。

1 个答案:

答案 0 :(得分:1)

我想这样的事情对你有用:

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;

具有双'foreach'循环的替代版本应该以相同的方式工作,但它不会创建冗余的元组数组:

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;