开发者

Return IQueryable DataTable before SQL execution?

I may have a misunderstanding but I was under the impression that havi开发者_如何学编程ng a IQueryable object which you can the do further filtering/ordering on only executes the relevant SQL once this is used in an enumerator if some sort eg/

IQueryable<Person> people = Person.All();
var peeps = people.Where(x = > x.Name = "John" && x.Surname == "Smith");
//At this point it generates a SQL and hits the DB?????
foreach (var person in peeps)  
{
...
}

Is it therefore possible to do this with a DataTable somehow? Instead of executing a SQL statement and putting it in a DataTable and then filtering out afterwards, you do the filtering and then it executes the SQL once enumerated over. This would obviously prevent all the data being returned to start with.

//Normal DataAdapter/DataTable fill approach
SqlDataAdapter da = new SqlDataAdapter(sql, cn);
da.Fill(dtConfig);
var result dtConfig.Rows.Cast<DataRow>()
      .Where(x => x.ItemArray.Any(
             y => y.ToString().IndexOf(param.sSearch, StringComparison.OrdinalIgnoreCase) >= 0));  //Search all columns
foreach(var row in result)
   {
     ....
   }

I don't think its possible, hopefully you can prove otherwise but thought it might be quite neat if it could be done. I guess the problem is that the SQL will need to know what columns should be filtered on etc however I guess you could populate a DataTable via FillSchema then do the filtering/ordering and then the SQL gets executed on the enumeration?


No; with DataTable it is the Fill operation that loads the data. After that, any LINQ you are doing is LINQ-to-Objects against the data already in memory. And I expect you are actually working against IEnumerable<T>, not IQueryable<T>.

Even if you called .AsQueryable(), that is still only a smoke-and-mirrors trick to expose LINQ-to-Objects as IQueryable<T> (useful for code re-use) - it is still LINQ-to-Objects, and still operates only on the in-memory data (IQueryable<T> offers the possibility of composed execution - not the promise of it).


This isn't possible as DataTable.Fill fetches the data directly from the database. See msdn under Remarks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜