none
total records RRS feed

  • Question

  • I'm learning new Entity Framework, where do I find the total number of records;

            public virtual IList<T> GetCount()
            {
                List<T> _count = new List<T>();
                using (var context = new DataContext())
                {
                    _count = context.Set<T>().Count();
                }
                return _count;
            }
    Thanks;


    • Edited by Kenan YILMAZ Wednesday, September 2, 2015 7:40 PM
    Wednesday, September 2, 2015 7:39 PM

Answers

  • I solved the problem as follows

           public virtual int GetCount()
            {
                using (var context = new DataContext())
                {
                    var dbSet = context.Set<T>();
                    return dbSet.Count();
                }
            }

    • Marked as answer by Kenan YILMAZ Wednesday, September 2, 2015 9:06 PM
    Wednesday, September 2, 2015 9:06 PM
  • Hello Kenan,

    >> I'm learning new Entity Framework, where do I find the total number of records;

    The DbSet<T> has a CountAsync<TSource> extended method which could be used to return all found records:

    https://msdn.microsoft.com/en-US/library/gg679592(v=vs.113).aspx

    There also some other approaches you could try:

    Get total row count in Entity Framework

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Kenan YILMAZ Thursday, September 3, 2015 7:09 PM
    Thursday, September 3, 2015 8:29 AM
    Moderator

All replies

  • I solved the problem as follows

           public virtual int GetCount()
            {
                using (var context = new DataContext())
                {
                    var dbSet = context.Set<T>();
                    return dbSet.Count();
                }
            }

    • Marked as answer by Kenan YILMAZ Wednesday, September 2, 2015 9:06 PM
    Wednesday, September 2, 2015 9:06 PM
  • Hello Kenan,

    >> I'm learning new Entity Framework, where do I find the total number of records;

    The DbSet<T> has a CountAsync<TSource> extended method which could be used to return all found records:

    https://msdn.microsoft.com/en-US/library/gg679592(v=vs.113).aspx

    There also some other approaches you could try:

    Get total row count in Entity Framework

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Kenan YILMAZ Thursday, September 3, 2015 7:09 PM
    Thursday, September 3, 2015 8:29 AM
    Moderator
  • Hi Fred;
    Thanks for the reply;
    • Marked as answer by Kenan YILMAZ Thursday, September 3, 2015 7:09 PM
    • Unmarked as answer by Kenan YILMAZ Thursday, September 3, 2015 7:09 PM
    Thursday, September 3, 2015 7:09 PM