Tridip Bhattacharjee

Tridip Bhattacharjee

  • NA
  • 1.2k
  • 74.1k

When to use AsEnumerable() along with EF to fetch data

Feb 12 2018 3:08 AM
please see the below code
  1. private IEnumerable<AutoCompleteData> GetAutoCompleteData(string searchTerm)  
  2. {  
  3. using (var context = new AdventureWorksEntities())  
  4. {  
  5. var results = context.Products  
  6. .Include("ProductSubcategory")  
  7. .Where(p => p.Name.Contains(searchTerm)  
  8. && p.DiscontinuedDate == null)  
  9. .AsEnumerable()  
  10. .Select(p => new AutoCompleteData  
  11. {  
  12. Id = p.ProductID,  
  13. Text = BuildAutoCompleteText(p)  
  14. })  
  15. .ToArray();  
  16. return results;  
  17. }  

please tell me why someone use .AsEnumerable() before select?

i use EF to fetch data but i never use .AsEnumerable() before select but some one used. it means there must be some reason to use there .AsEnumerable().

please discuss the significance of using .AsEnumerable() before select there?

if we remove .AsEnumerable() from EF query then does it execute and return data ?

here is my example for fetching data where i have not used AsEnumerable()
  1. var customer = (from s in db.Customers  
  2. // select s;  
  3. select new CustomerDTO  
  4. {  
  5. CustomerID = s.CustomerID,  
  6. CompanyName = s.CompanyName,  
  7. ContactName = s.ContactName,  
  8. ContactTitle = s.ContactTitle,  
  9. Address = s.Address  
  10. }); 


thanks

Answers (3)