none
Why Entity Framewok method “.AsEnumerable()” returns nothing? RRS feed

  • Question

  • I use Entity Framework 6.0.0.0, and approach is Database First. I have three tables ClientAccountDoc. One Client has many Docs and Accounts. The relationship is one to manyClient table:

    public partial class Client
    {
        public Client()
        {
            this.Account = new HashSet<Account>();
            this.Doc = new HashSet<Doc>();
        }
    
        public int ClientId { get; set; }
        public string Name { get; set; }
    
        public virtual ICollection<Account> Account { get; set; }
        public virtual ICollection<Doc> Doc { get; set; }
    }

    AngularJs code snippet to take data from WebApi:

    angular.module("app", []).controller("searchController", function ($scope, $http) {
    
        //Used to get and display the data
        $http.get('/api/Search/').success(function (data) {
            debugger;
            $scope.searchs = data;
            $scope.loading = false;
        })
        .error(function () {
            debugger;
            $scope.error = "An Error has occured while loading posts!";
            $scope.loading = false;
        });
    }

    Method Get of Web API to take data(it returns NOTHING. There is no clients):

        [HttpGet]
        public IEnumerable<Client> Get()
        {
            private ClientDBEntities db = new ClientDBEntities();
            retutn db.Client.AsEnumerable();// it returns NOTHING. There is no clients
    
        }

    However, $http.get('/api/Search/').success(function (data) called, if I change this method to return IEnumerable<string> :

        [HttpGet]
        public IEnumerable<Client> Get()
        {
            List<string> list = new List<string>(){"1", "2", "3"};
            return list;            
        }

    My question is why db.Client.AsEnumerable() returns nothing? I've tried to change this code to:

    retutn db.Client.ToList();// it returns all necessary data

    However, AngularJS method is calling $http.get('/api/Search/').error(...)

    Any help would be greatly appreciated.

    What I see in a Watch window of Visual Studioenter image description here

    Thursday, August 25, 2016 9:57 AM

Answers

  • >>My question is why db.Client.AsEnumerable() returns nothing?

    Because the AsEnumerable<TSource>(IEnumerable<TSource>) method has no effect other than to change the compile-time type of source from a type that implements IEnumerable<T> to IEnumerable<T> itself: https://msdn.microsoft.com/en-us/library/bb335435(v=vs.110).aspx.

    It is just a casting/conversion thing, it doesn't fetch the items from the database. Please refer to the following page and the documentation above for more information: http://stackoverflow.com/questions/17968469/whats-the-differences-between-tolist-asenumerable-asqueryable

    You should call the database and return the items that you get back from the DB in your service method. Calling .ToList() will fetch the items from the DB:

    [HttpGet]
        public IEnumerable<Client> Get()
        {
            using(ClientDBEntities db = new ClientDBEntities())
              retutn db.Client.ToList(); //execute the query against the database here
    
        }
    

    For questions any questions you may have about AngularJS and web development in general, you should ask at http://forums.asp.net.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by NiceStepUp Thursday, August 25, 2016 9:34 PM
    Thursday, August 25, 2016 6:37 PM

All replies

  • http://www.dotnetperls.com/asenumerable

    To be honest, I have only seen AsEnumerable used with Linq in querying a Datatable.

    Thursday, August 25, 2016 5:43 PM
  • >>My question is why db.Client.AsEnumerable() returns nothing?

    Because the AsEnumerable<TSource>(IEnumerable<TSource>) method has no effect other than to change the compile-time type of source from a type that implements IEnumerable<T> to IEnumerable<T> itself: https://msdn.microsoft.com/en-us/library/bb335435(v=vs.110).aspx.

    It is just a casting/conversion thing, it doesn't fetch the items from the database. Please refer to the following page and the documentation above for more information: http://stackoverflow.com/questions/17968469/whats-the-differences-between-tolist-asenumerable-asqueryable

    You should call the database and return the items that you get back from the DB in your service method. Calling .ToList() will fetch the items from the DB:

    [HttpGet]
        public IEnumerable<Client> Get()
        {
            using(ClientDBEntities db = new ClientDBEntities())
              retutn db.Client.ToList(); //execute the query against the database here
    
        }
    

    For questions any questions you may have about AngularJS and web development in general, you should ask at http://forums.asp.net.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Marked as answer by NiceStepUp Thursday, August 25, 2016 9:34 PM
    Thursday, August 25, 2016 6:37 PM