To Thread or Not To Thread Comments

Building a Linq Query gives me a question.

3. April 2008 23:12 by scott in   //  Tags: , , ,   //   Comments (5)

So here I am building a new site that will estimate around 500,000 page views and 50,000 database transactions DAILY.  I am currently working on a simple query that selects ONE row[column] from a database and returns the value to the rest of the methods.  I have a simple thought or question I would like answered and thought this would be a place to bring it up.

Is the foreach loop required even if I know I will receive only 1 record back from the database?

That's it and to give you an example:

        CS_Code.DataContext db = new CS_Code.DataContext(SQLStatementsCS.ConnectionStringID());
        var query = from RN in db.Province
                    where RN.Name == Name
                    select RN.uid;

        int value;
        foreach (var uid in query)
        { value = uid; }


I think it is kind of pointless to create a foreach loop and use 3 lines of code to just get an ID out of the database. So let me know in the comments section if this is the only answer and I will point out the answer and give you a link back to a site of your choice.

Thanks guys and dolls.

kick it on DotNetKicks.com

Comments (5) -

VidalSasoon
VidalSasoon
4/7/2008 8:33:26 AM #

Maybe you need "Take"?


weblogs.asp.net/.../...NET-_2800_Part-1_2900_.aspx
====================
       TravelOrganizer travel = new TravelOrganizer();

         GridView1.DataSource = (from location in travel.PlacesVisited
                               orderby location.Distance descending
                               select location).Skip(1).Take(5);
        GridView1.DataBind();

Scott
Scott
4/7/2008 8:41:12 AM #

The problem with that, is I am not using a Girdview and only returning one row and one row only.  So what you just gave me won't help.  Sorry.

Michael Reyeros
Michael Reyeros
4/7/2008 10:24:12 AM #

You could try using the Single Linq operator:
db.Provinces.Single(p => p.Name == Name);

justin
justin
4/7/2008 12:01:38 PM #

int value = (from RN in db.Province
                    where RN.Name == Name
                    select RN.uid).FirstOrDefault();

Scott
Scott
4/19/2008 10:19:50 AM #

Thanks Justin. That works. I appreciate the help guys.

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading