Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > LINQ
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
LINQ Discuss Microsoft's LINQ (Language INtegrated Query) for .NET 3.0 and later.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the LINQ section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 16th, 2011, 01:49 PM
Friend of Wrox
Points: 2,813, Level: 22
Points: 2,813, Level: 22 Points: 2,813, Level: 22 Points: 2,813, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 427
Thanks: 57
Thanked 2 Times in 2 Posts
Default Retrieving a single value from a single column in a single row?

Is there some simple way to reference a single value from a single column in a single row of a LINQ result set? I don't want to bind it to a control. I don't want to iterate through every row in the set. I just want the equivalent of this:

dim strTemp as string = datatable.row(0)("FieldName")
  #2 (permalink)  
Old May 16th, 2011, 04:25 PM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,052
Thanks: 80
Thanked 1,581 Times in 1,558 Posts
Default

Hi Ron,

You can use projection to select only the "column" or property of an object. You can then use FirstOrDefault (or First) to only get a single value. The following code would store Ron in the singleFirstName.FirstName variable:

Code:
 
Public Class Form1
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim list As New List(Of Person) From {New Person() With {.FirstName = "Imar", .LastName = "Spaanjaars"}, New Person() With {.FirstName = "Ron", .LastName = "Howerton"}}
    Dim singleFirstName = (From p In list
                          Where p.LastName = "Howerton"
                          Select New With {.FirstName = p.FirstName}).FirstOrDefault()
  ' singleFirstName.FirstName = Ron
  End Sub
End Class
Public Class Person
  Public Property FirstName As String
  Public Property LastName As String
End Class
This code "projects" the first name property into a new, anonymous object with a single property called FirstName.

You can also assign it to a string variable directly by just selecting the FirstName field, like this:

Code:
Dim singleFirstName = (From p In list
                          Where p.LastName = "Howerton"
                          Select p.FirstName).FirstOrDefault()
If you anticipate multiple results, drop the call to FirstOrDefault and loop over the values in singleFirstName (which then of course has an odd name ;-) ).

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
Ron Howerton (May 17th, 2011)
  #3 (permalink)  
Old May 17th, 2011, 08:46 AM
Friend of Wrox
Points: 2,813, Level: 22
Points: 2,813, Level: 22 Points: 2,813, Level: 22 Points: 2,813, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 427
Thanks: 57
Thanked 2 Times in 2 Posts
Default

Right on the first response, as always, Imar! I knew there had to be a simple answer, but all the suggestions I found looked as complex as using SQL Client objects.

Thanks so much!
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Storing checkboxes value in a single row abhishekkashyap27 C# 2005 2 April 8th, 2008 12:13 AM
Consolidating multiple occurences in a single row ssray23 SQL Language 11 May 18th, 2007 11:00 AM
Converting large columns into a single row rahulpokharna SQL Server 2000 3 January 10th, 2006 01:14 AM
retrieving single value from database Moharo PHP How-To 2 July 5th, 2005 03:29 AM
ASP.Net Single Single-on with Oracle Application S guhanath Oracle 0 October 6th, 2004 05:05 AM



All times are GMT -4. The time now is 06:50 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.