Question

I have a GridView which needs to page and sort data which comes from a collection of Customer objects.

Unfortunately my customer information is stored separately...the customer information is stored as a Customer ID in my database, and the Customer Name in a separate DLL.

I retrieve the ID from the database using Entity Framework, and the name from the external DLL through a partial class.

I am getting the ID from my database as follows:

 public class DAL
 {
    public IEnumberable<Customer> GetCustomers()
    {
      Entities entities = new Entities();
      var customers = (from c in entities.Customers
                       select c);

                //CustomerID is a field in the Customer table

      return customers;
     }
}

I have then created a partial class, which retrieves the data from the DLL:

public partial class Customer
{
    private string name;

    public string Name
    {
        if (name==null)
        {
            DLLManager manager = new DLLManager();
            name= manager.GetName(CustomerID);
        }

         return name;
     }   
}

In my business layer I can then call something like:

public class BLL
{
    public List<Customer> GetCustomers()
    {
          DAL customersDAL = new DAL();
          var customers = customersDAL.GetCustomers();
          return customers.ToList();
     }
}

...and this gives me a collection of Customers with ID and Name.

My problem is that I wish to page and sort by Customer Name, which as we have seen, is populated from a DLL. This means I cannot page and sort in the database, which is my preferred solution. I am therefore assuming I am going to have to call of the database records into memory, and perform paging and sorting at this level.

My question is - what is the best way to page and sort an in-memory collection. Can I do this with my List in the BLL above? I assume the List would then need to be stored in Session.

I am interested in people's thoughts on the best way to page and sort a field that does not come from the database in an Entity Framework scenario.

Very grateful for any help!

Mart

p.s. This question is a development of this post here: GridView sorting and paging Entity Framework with calculated field The only difference here is that I am now using a partial class, and hopefully this post is a little clearer.

Was it helpful?

Solution 2

I posted this question slightly differently on a different forum, and got the following solution.

Basically I return the data as an IQueryable from the DAL which has already been forced to execute using ToList(). This means that I am running my sorting and paging against an object which consists of data from the DB and DLL. This also allows Scott's dynamic sorting to take place.

The BLL then performs OrderBy(), Skip() and Take() on the returned IQueryable and then returns this as a List to my GridView.

It works fine, but I am slightly bemused that we are perfoming IQueryable to List to IQueryable to List again.

1) Get the results from the database as an IQueryable:

public class DAL
{
    public IQueryable<Customer> GetCustomers()
    {
      Entities entities = new Entities();
      var customers = (from c in entities.Customers
                       select c);

            //CustomerID is a field in the Customer table

      return customers.ToList().AsQueryable();
    }
}

2) Pull the results into my business layer:

public class BLL
{
    public List<Customer> GetCustomers(intint startRowIndex, int maximumRows, string        sortParameter)
    {
      DAL customersDAL = new DAL();
      return customersDAL.GetCustomers().OrderBy(sortParameter).Skip(startRowIndex).Take(maximumRows).ToList();
     }
}

Here is the link to the other thread.

http://forums.asp.net/p/1976270/5655727.aspx?Paging+and+sorting+Entity+Framework+on+a+field+from+Partial+Class

Hope this helps others!

OTHER TIPS

Yes, you can page and sort within you list in the BLL. As long as its fast enough I wouldn't care to much about caching something in the session. An other way would be to extend your database with the data from you DLL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top