Why we need? Let’s take a scenario. Assume that you have created one class which is returning all the customers and you want to give ability to query on those customers to the developers. As a Technical Leader, in BLL layer, you have created one repository class and given to the team. But you are not aware that on which page what type of filters they need. So the best solution is to give some power to developer, so that they can also query on that data.
Here is the simple example/implementation of IQueryable<T>. You can copy and paste in your visual studio to understand more.
protected void Page_Load(object sender, EventArgs e) { using(CustomerRepository cr = new CustomerRepository()) { IQueryable<Customer> fCust = cr.getCustomers() .Where(c => c.City_Id > 2); foreach (var item in fCust) { Response.Write(string.Format( "<br>Customer {0}'s cityId is {1}", item.Name, item.City_Id)); } } } public class CustomerRepository : IDisposable { IQueryable<Customer> iCust; MyTestEntities db; public CustomerRepository() { db = new MyTestEntities(); } public IQueryable<Customer> getCustomers() { iCust = (from Customer c in db.Customers select c); return iCust; } public void Dispose() { db.Dispose(); } }
No comments:
Post a Comment