See if record exists without looping?

Posted on 2005-04-20
Last Modified: 2012-06-27
Is there a built-in function to determine whether a value exists in a DataSet?  Here's my DataSet:

    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter ("SELECT DISTINCT d_date FROM mytable", conn);

I want to know if a given date exists in the above DataSet, and I'd prefer not to loop through all the records because I need to check for dates many times and have to loop each time.  I'm hoping there's a more efficient way.  For example, something like:

   bool exists = (ds.Tables[0].Rows.FindByValue("04/01/2005") > -1);

...or something similar (this is just an example).  'exists' would be true if it finds the value in the DataSet, else false.  Or am I forced to loop through each row to find out if it exists?  Like so:

private bool CheckForDate(string sDate) {
    foreach(DataRow dr in ds.Tables[0].Rows) {
        if(dr[0].ToString() == sDate) return true;
    return false;
Question by:hendridm
    LVL 3

    Accepted Solution

    You can use the Tables[0].DefaultView.Find() or .FindRows().  I would run sort on the column first, but if I'm not mistaken, both methods will search the entire row for the value you supply.  Keep in mind that find and findrows methods do not except wildcards, so you're input must be exact.

    Also it sometimes helps if you have more than one column to set the sort to the column you want to search through (if you know it of course).


    ds.Tables[0].DefaultView.Sort = "columnname"; // if more than one column
    ds.Tables[0].DefaultView.FindRows("valuetofind"); // this returns an array of DataRowView objects if any are found.


    ds.Tables[0].DefaultView.Find("valuetofind"); // returns the index number of the row if found.

    Hope this helps.

    LVL 8

    Author Comment

    Perfect.  Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now