What is the cheapest way to execute a LINQ-Query immediately

后端 未结 2 1915
萌比男神i
萌比男神i 2021-01-23 05:17

This question came into my mind while generating sample data for a SO-answer. I don\'t like the verbose way of adding DataRows one by one via Tbl.Rows.Add, so i\'ve

相关标签:
2条回答
  • 2021-01-23 05:51

    I'd just rewrite it as a foreach:

    For Each y As String in years
        tbl.Rows.Add(y)
    Next
    

    It's much more clear what your intention is this way, and it executes right away.

    0 讨论(0)
  • 2021-01-23 06:15

    I'd be more tempted to use a List(Of T):

    Dim years As New List(Of String) From {"2010/2009", "2009/2008", "2008/2007", "2007/2006", "2006/2005", "2005/2004", "2004/2003"}
    
    years.ForEach(Sub(y) tbl.Rows.Add(y))
    

    It's all subjective though, loops are probably most clear for these one liners.

    0 讨论(0)
提交回复
热议问题