Reputation: 31
I build a asp.net Web Application Webforms with Entity Framework, I find Two Way to bind the ListView for: 1- By DataSource Like this
void Bind()
{
var search = db.Search.Where(k => k.RequestId == RequestId);
lstSearch.DataSource = search.ToList();
lstSearch.DataBind();
}
2- By use SelectMethod Like this
public IQueryable<Search> BindOrders()
{
var search = db.Search.Where(k => k.RequestId == 12).AsQueryable();
return search;
}
which one is best and why?
Upvotes: 1
Views: 830
Reputation: 826
SelectMethod
and many other features for binding data to web controls were introduced in .NET Framework 4.5
as a strongly-typed data-binding. Those features let's you handle the data you create/delete/modify/filter from/to web controls in a clean and maintainable way.
In the other hand DataSource
way is the old way of binding data to web controls.
I advise you to read this blog which goes in detail about the subject (ScottGu's):
The new Model Binding support in ASP.NET vNext is a nice evolution of the existing Web Forms data-binding system. It borrows concepts and features from the Model Binding system in ASP.NET MVC (you'll see this more in later posts), and makes working with code-focused data-access paradigms simpler and more flexible.
Also, check for the advantages of using IQueryable<Object>
:
The main difference, from a user's perspective, is that, when you use IQueryable (with a provider that supports things correctly), you can save a lot of resources.
Upvotes: 2