none
Limit results returned in SQL query RRS feed

  • Question

  • I have been trying to figure out how I can limit the results returned from my query to 10. The only thing I have been able to find is this

    var rows = ds.Tables[0].AsEnumerable().Take(10);

    But the Take() function does not seem to exist in .NET 4.5

    Here is my code:

            public DataTable getAnswerTextDetails(int questionID, int maxDisplay)
            {
                //var
                DataRow[] selectedData = null;
                DataTable questionDetails = null;
                //var
    
                if (surveyReportSet.Tables["detailsText"] != null)
                {
                    selectedData = surveyReportSet.Tables["detailsText"].Select("questionid=" + questionID.ToString());
                    if (selectedData.Length > 0)
                        questionDetails = selectedData.CopyToDataTable();
                };
    
                return questionDetails;
            }

    Is there no way to just do something simple like:

    selectedData = surveyReportSet.Tables["detailsText"].Select("questionid=" + questionID.ToString()).limit(10);

    Monday, June 9, 2014 6:14 PM

Answers

  • But the Take() function does not seem to exist in .NET 4.5

    According to the documentation, there should be a Take() extension method on IEnumerable in 4.5:

    http://msdn.microsoft.com/en-us/library/vstudio/bb503062(v=vs.110).aspx

    Have you remembered to add "using System.Linq;" to your code?

    If you need it, is is easy to write a Take() extension:

    public static class Extensions
    {
        public static IEnumerable<T> Take<T>(
    	this IEnumerable<T> source,
    	int count)
        {
            List<T> list = new List<T>();
            int i=0;
            foreach (T item in source)
            {
                if (++i > count) break;
                list.Add(item);
            }
            return list;
        }
    }
    

    • Marked as answer by josh.net Monday, June 9, 2014 9:43 PM
    Monday, June 9, 2014 8:08 PM
    Moderator

All replies

  • But the Take() function does not seem to exist in .NET 4.5

    According to the documentation, there should be a Take() extension method on IEnumerable in 4.5:

    http://msdn.microsoft.com/en-us/library/vstudio/bb503062(v=vs.110).aspx

    Have you remembered to add "using System.Linq;" to your code?

    If you need it, is is easy to write a Take() extension:

    public static class Extensions
    {
        public static IEnumerable<T> Take<T>(
    	this IEnumerable<T> source,
    	int count)
        {
            List<T> list = new List<T>();
            int i=0;
            foreach (T item in source)
            {
                if (++i > count) break;
                list.Add(item);
            }
            return list;
        }
    }
    

    • Marked as answer by josh.net Monday, June 9, 2014 9:43 PM
    Monday, June 9, 2014 8:08 PM
    Moderator
  • Thank you! I resolved my issue

    IEnumerable<DataRow> topSelected = null;
    topSelected = selectedData.Take(maxDisplay);

    Monday, June 9, 2014 9:45 PM
  • Using Linq

                List<DataRow>  selectedData = surveyReportSet.Tables["detailsText"].AsEnumerable()
                        .Where(x => x.Field<string>("questionid") == questionID.ToString())
                        .Take(10).ToList();
    


    jdweng

    Tuesday, June 10, 2014 1:05 AM