Click here to Skip to main content
12,826,820 members (38,681 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 15 Jul 2010

Generic LINQ Usage To Return Single Value From DataRow

, 15 Jul 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip will allow you to return the value from column X, where column Y matches your filter
I've used LINQ to objects for ever, but have stayed away from LINQ to sql for a while, because we don't exclusive use SQL Server.

I recently began toying with the System.Data.DataSetExtensions, and realized LINQ was a much faster (easier to code anyways) way to get the value from a DataColumn out of a DataTable.

Let's say I have a table of persons names, and I want to get the last name where the person_id = 100. I used to do something like this:

var persons = new DataTable();

string lastName = string.Empty;

DataRow[] foundRows = persons.Select("Person_Id = 100");
if (foundRows.Length == 1)
    lastName = foundRows[0]["Last_Name"].ToString();

now with LINQ, I have created the following method:
public static T GetFirstResultValue<t,ty>(DataTable table,string colToSearch, string colToReturn, TY searchValue) where TY: IComparable
    T ret = default(T);

    IEnumerable<datarow> rows = from row in table.AsEnumerable()
                                        where row.Field<ty>(colToSearch).CompareTo(searchValue) == 0
                                        select row;

    if (rows.Count() == 1)
        ret = (T) rows.First()[colToReturn];

    return ret;

This particular method will only work if the results of the search return 1 row, but it could easily be tweaked to return the DataRow collection, or an IEnumerable list of column values for the found rows.

Hope this helps someone!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Paul Brower
Choice Genetics US
United States United States
Seasoned IT Professional. Currently the IT Director for Choice Genetics, and also the world-wide manager for IT Projects related to R&D for Groupe Grimaud (our parent company).

I've spent about half my career as a contractor. I've lived all over the place, but currently reside near St. Louis, Missouri. Moved out here from Roseville, California in Feb-2005. No regrets yet.

Over the recent years I've written software for:
- Disposing of radioactive and toxic waste.
- Disposing of surplus inventory during the Decommission of McClellan Air Force Base.
- Facilitating genetic improvement for Swine Breeding.
- Managing children placed in State custody.
- Dealing with commercial trucking delivery schedules.
- Tracking high resolution images from archeological digs.
- Project Management for the roofing industry.
- Processing engines for credit card transactions.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170326.1 | Last Updated 15 Jul 2010
Article Copyright 2010 by Paul Brower
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid