如何根据linq中的返回结果返回多余的“空白”行?

时间:2018-09-07 09:03:50

标签: .net vb.net linq

是否有一种方法可以合并以下两个Linq?我尝试使用Union方法

dim result = query2.Union(query3).ToList() 

但是我得到了错误

  

无法转换类型的对象   'WhereSelectEnumerableIterator 2[VB$AnonymousType_4 3 [System.Object,System.Object,System.Collections.Generic.IEnumerable`

我只想在query2中的每个返回行上都有一个额外的空白行,因此,如果查询2返回两行,则应该有两个空白行,我尝试在带有query union的query2上添加另一个选择,但出现错误消息。

Dim query2 = (From e In dsSummary.Tables(0).AsEnumerable()
              Group e By
                  DistNum = e("DistNum"),
                  DistName = e("DistName")
              Into Group
              Select New With {
                  .DistNum = DistNum,
                  .DistName = DistName,
                  .EventName = "Distributor Number Total",
                  .DiscountTotal = Group.Sum(Function(x) x.Field(Of Decimal)("DiscountTotal")).ToString("C2"),
                  .NetTotal = Group.Sum(Function(x) x.Field(Of Decimal)("NetTotal")).ToString("C2"),
                  .SummaryRow = "Y"
              })

Dim query3 = (From e In dsSummary.Tables(0).AsEnumerable()
            Group e By
                DistNum = e("DistNum"),
                DistName = e("DistName")
            Into Group
            Select New With {
                .DistNum = DistNum,
                .DistName = "",
                .EventName = "Distributor Number Total",
                .DiscountTotal = 0.ToString("C2"),
                .NetTotal = 0.ToString("C2"),
                .SummaryRow = "Z"
            })

1 个答案:

答案 0 :(得分:2)

如果您只想在末尾添加“空行”,请在查询中为每行添加一个:

Dim emptyRows = From x In query2
                Select New With {.DistNum = "", .DistName = "", .EventName = "", .DiscountTotal = "", .NetTotal = "", .SummaryRow = ""}
Dim resultList = query2.Concat(emptyRows).ToList()

如果您不希望它们添加到末尾,而是使用SummaryRow进行排序,请将OrderBy放在ToList()之前。