Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > LINQ
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old May 16th, 2011, 01:49 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 428
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")
Old May 16th, 2011, 04:25 PM
Imar's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

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:

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:

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 Spaanjaars
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)
Old May 17th, 2011, 08:46 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts

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!

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 02: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

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.