Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7172389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:36:20+00:00 2026-05-28T15:36:20+00:00

This question came into my mind while generating sample data for a SO-answer. I

  • 0

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 created this pseudo LINQ query that does nothing but adding rows implicitely:

Private Function GetData() As DataTable
    Dim years = {"2010/2009", "2009/2008", "2008/2007", "2007/2006", "2006/2005", "2005/2004", "2004/2003"}
    Dim tbl = New DataTable
    tbl.Columns.Add(New DataColumn("Year"))
    ' adds DataRows to the DataTable, need Count to execute the query '
    Dim c = (From y In years Select tbl.Rows.Add(y)).Count 
    Return tbl
End Function

As you can see i don’t need the result of the query at all, it’s only purpose is to iterate and call DataTable.Rows.Add. So the result is already available in the DataTable itself and not needed from the query.

Admittedly this question is somewhat hyphotetical because there would not be a big difference in using f.e. ToList and normally a query’s purpose is to return something.

But anyway, what’s the cheapest way(in terms of memory consumption, execution time) to execute a LINQ query when only the execution and not the result matters?

Edit: Ok, thisquestion was a quick-shot and a chicken-and-egg problem. I wanted to reduce 20 lines of code to a single line but noticed that i need some kind of DataSource for the LINQ-Query. Therefor i’ve created the Array. But at this point i could have simply used a for-each loop as well to add the DataRows.

Conclusion: Use a LINQ-Query for what it’s for: a query. Then this question is pointless since there is no cheapest way but only that which returns the correct result.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:36:22+00:00Added an answer on May 28, 2026 at 3:36 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So here I am tonight with this question that came up into my mind
This question came to my mind when I learned C++ with a background of
In trying to answer this question I came up with the following code: #include
This question has just came in to my mind, and I'm not in a
This question came today in the manipulatr mailing list. http://groups.google.com/group/manipulatr/browse_thread/thread/fbab76945f7cba3f I am rephrasing. Given
This question came about because the cells gem specifies template directories using File.join('app','cells'). That
let me tell you a bit about where this question came from. I have
Problem This question actually came up at work today. We are planning an experiment
I just came across this question about initializing local variables. Many of the answers
Today I came across this question: you have a code static int counter =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.