对部分类中的字段进行分页和排序实体框架

问题描述 投票:0回答:2

我有一个 GridView,它需要对来自 Customer 对象集合的数据进行分页和排序。

不幸的是,我的客户信息是单独存储的...客户信息作为客户 ID 存储在我的数据库中,客户名称存储在单独的 DLL 中。

我使用实体框架从数据库中检索 ID,并通过分部类从外部 DLL 中检索名称。

我从数据库中获取 ID,如下所示:

 public class DAL
 {
    public IEnumerable<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;
     }
}

然后我创建了一个分部类,它从 DLL 中检索数据:

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

         return name;
     }   
}

在我的业务层中,我可以调用类似的内容:

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

...这为我提供了包含 ID 和姓名的客户集合。

我的问题是我希望按客户名称进行分页和排序,正如我们所见,客户名称是从 DLL 填充的。这意味着我无法在数据库中进行分页和排序,这是我的首选解决方案。因此,我假设我必须将数据库记录调用到内存中,并在此级别执行分页和排序。

我的问题是 - 对内存集合进行分页和排序的最佳方法是什么。我可以使用上面 BLL 中的列表来执行此操作吗?我假设列表需要存储在会话中。

我对人们关于在实体框架场景中对非来自数据库的字段进行分页和排序的最佳方式的想法感兴趣。

非常感谢您的帮助!

商城

PS:这个问题是这篇文章的发展: GridView 使用计算字段排序和分页实体框架 这里唯一的区别是我现在使用的是部分类,希望这篇文章更清晰一些。

entity-framework gridview pagination
2个回答
0
投票

是的,您可以在 BLL 中的列表中进行分页和排序。只要它足够快,我就不会太关心在会话中缓存某些内容。另一种方法是使用 DLL 中的数据扩展数据库。


0
投票

我在不同的论坛上发布了这个问题,略有不同,并得到了以下解决方案。

基本上,我从 DAL 以 IQueryable 形式返回数据,该数据已强制使用 ToList() 执行。这意味着我正在针对一个由 DB 和 DLL 中的数据组成的对象运行排序和分页。这也使得 Scott 的动态排序得以发生。

BLL 然后对返回的 IQueryable 执行 OrderBy()、Skip() 和 Take(),然后将其作为列表返回到我的 GridView。

它工作正常,但我有点困惑,我们再次执行 IQueryable to List 到 IQueryable to List。

1)以 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)将结果拉入我的业务层:

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();
     }
}

这是另一个线程的链接。

http://forums.asp.net/p/1976270/5655727.aspx?分页+和+排序+Entity+Framework+on+a+field+from+Partial+Class

希望这对其他人有帮助!

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.