0

I am using visual studio 2013 .Here a strange thing happen but i cannot find the answer .I have following LINQ query

var tvAssingedJobs = dbEntities.Tv_ProductService
   .Where(i => i.ServiceManagerId == userId 
       && i.TechnicianId != null 
       && (i.ProductServiceStatus != "C" 
       && i.ProductServiceStatus != "QP")
   );
foreach (var assingedJob in tvAssingedJobs)
{
    var requision = assingedJob.Tv_SparePartsRequision
       .FirstOrDefault(i => i.RecommendedUserId == userId 
           && i.TechnicianId == assingedJob.TechnicianId
       );
}

Here i can iterate but i did not convert it to list .Is this query supposed to be like this

var tvAssingedJobs = dbEntities.Tv_ProductService
    .Where(i => i.ServiceManagerId == userId 
         && i.TechnicianId != null 
         && (i.ProductServiceStatus != "C" 
         && i.ProductServiceStatus != "QP")
     ).ToList();

Both working fine.So my question in which to use ? Is first query may cause exception or create some performance issue in production in future ?

Mostafiz
  • 7,243
  • 3
  • 28
  • 42
Syed Mhamudul Hasan
  • 1,341
  • 2
  • 17
  • 45
  • 1
    probably u should check difference between list and ienumerable for more clarity – Sandip Bantawa Apr 23 '16 at 04:40
  • With `ToList` you force immediate execution of the whole query and you get all the results back to work with them in memory. Without it, you're just building the query which you can refine more and more until you only send a very specific statement to the database and only get the results back you need. See [IEnumerable vs List](http://stackoverflow.com/questions/3628425/ienumerable-vs-list-what-to-use-how-do-they-work) and [IEnumerable vs IQueriable](http://stackoverflow.com/questions/2876616/returning-ienumerablet-vs-iqueryablet) for more information. – Corak Apr 23 '16 at 04:50
  • Thats the answer i am looking for.Thankz .Please give it as answer so that i can accept it @Corak – Syed Mhamudul Hasan Apr 24 '16 at 06:08

0 Answers0