Last post Jan 08, 2015 08:12 AM by bdassow
Jan 07, 2015 05:58 PM|bdassow|LINK
In this situation I am not using Entity Queries and I am not displaying all the SQL commands being used simply to keep the content in this question short. Below is my code in question.
Is this the correct way to convert a List to an IQueryable List?
List<Family> families = new List<Family>();
using (SqlConnection myConnection = new SqlConnection(connectionString))
SqlDataReader nwReader = selectTemp_DistinctFamilies_Command.ExecuteReader();
FamilyName = nwReader["lastName"].ToString(),
var showFamilies = families.ToList();
IQueryable getFamilies = showFamilies.AsQueryable();
Thanks in advance for your help.
Jan 08, 2015 01:02 AM|thaicarrot|LINK
To answer your question is "YES" for my opinion this is not the right way because of these code hit data base(every time you invoke it).
Do you know what is the difference between IQueryable<T> and IEnumerable<T>?
Interface returns collections as IEnumerable<T>, rather than IList<T> or ICollection<T>, to prevent the direct addition of entities to the collections. If ToList was not called, then the methods would return an IQueryable<T> and the database would not be
accessed until something iterated over the IQueryable<T> object. The problem with returning an IQueryable<T> is that a developer consuming the API is likely to assume that the query has already executed and that you are working with the results. If you iterate
over the query more than once, it will result in multiple calls to the database.
you'd better to create a wrapper method to wrap these codes and the method has to return
IEnumerable<T> then inside the method you'd better to call ToList(), finally GC will destroy those object after used it.
Jan 08, 2015 08:12 AM|bdassow|LINK
I fixed my own problem. I needed to add an additional SQL query in order for my code to work properly(seperate issue none related to this post). So if you are calling a method from a nested ListView that returns an IQuerayable list, the code I have is correct
for this situation.