使用Linq分析Gridview

时间:2015-09-04 21:06:32

标签: c# linq gridview

所以我有一个清单: -

static List<ListOfEmployees> employees = new List<ListOfEmployees> 
{ 
   new ListOfEmployees {Name = "John Doe", Address = "123 Test Ave S, Eugene, OR", Phone = "541-123-2345", DateOfHire = Convert.ToDateTime("2/23/2013")},
   new ListOfEmployees {Name = "Billy Rodney", Address = "551E 44th Ave, Eugene, OR", Phone = "541-123-1234", DateOfHire = Convert.ToDateTime("1/2/2014")},
   new ListOfEmployees {Name = "Arne Riise", Address = "99W Hwy21, Eugene, OR", Phone = "541-123-4466", DateOfHire = Convert.ToDateTime("6/11/2014")},
   new ListOfEmployees {Name = "Mike Duty", Address = "1450 Hilyard Lane, Eugene, OR", Phone = "541-123-9987", DateOfHire = Convert.ToDateTime("11/21/2014")},
   new ListOfEmployees {Name = "Nigel Poppet", Address = "771 Fox Run, Eugene, OR", Phone = "541-123-6655", DateOfHire = Convert.ToDateTime("5/9/2013")},
   new ListOfEmployees {Name = "Shawn Dot", Address = "1220 Ferry St, Eugene, OR", Phone = "541-123-3345", DateOfHire = Convert.ToDateTime("8/15/2014")},
   new ListOfEmployees {Name = "Man Child", Address = "PO Box #221, Eugene, OR", Phone = "541-123-9987", DateOfHire = Convert.ToDateTime("12/23/2014")},
   new ListOfEmployees {Name = "Mike Duty", Address = "1450 Hilyard Lane, Eugene, OR", Phone = "541-123-9987", DateOfHire = Convert.ToDateTime("11/21/2014")},
   new ListOfEmployees {Name = "Nigel Poppet", Address = "771 Fox Run, Eugene, OR", Phone = "541-123-6655", DateOfHire = Convert.ToDateTime("5/9/2013")},
   new ListOfEmployees {Name = "Shawn Dot", Address = "1220 Ferry St, Eugene, OR", Phone = "541-123-3345", DateOfHire = Convert.ToDateTime("8/15/2014")},
   new ListOfEmployees {Name = "Man Child", Address = "PO Box #221, Eugene, OR", Phone = "541-123-9987", DateOfHire = Convert.ToDateTime("12/23/2014")}
};

它属于ListOfEmployee类。

我正在学习linq并实现了一个简单的linq查询: -

private IEnumerable<ListOfEmployees> returnEmployees(DateTime userInputLimit, int rowIndex)
{
    var Employees =
    from e in employees
    where e.DateOfHire < userInputLimit
    select e;

    createEmployeesDataTable(Employees);

    // Paginate //
    Employees = Employees.Take(11).Skip(rowIndex);

    return Employees.ToList();
}

返回的结果绑定到我的aspx Gridview: -

private void submitQuery(int rowIndex)
{
    DateTime userInputLimit = Convert.ToDateTime(txtboxHireDate.Text);
    var Employees = returnEmployees(userInputLimit, rowIndex);
    gvw_Linq_Results.DataSource = Employees;

    gvw_Linq_Results.DataBind();
}

绑定后,我正在为Gridview实现Paging: -

protected void gvw_Linq_Results_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    int pageIndex = e.NewPageIndex;
    gvw_Linq_Results.PageIndex = pageIndex;
    submitQuery(pageIndex*gvw_Linq_Results.PageSize);
}

问题是,假设我有11名员工,我的gridview maxPageSize = 5.我立即在我的UI上显示3个页面索引。现在当我点击第二页时,我的UI显示正确显示第二页的结果,但在显示中,第一页丢失,第二页显示为第一页,第三页显示为第二页。 / p>

或者,如果我立即点击第3页,那么我的第1页和第2页都会从UI显示中丢失,我只得到1行1个网格页面(因为11记录 - 2页* 5记录/页)。 / p>

我不熟悉linq并跳过/采取技巧,并想知道是否有人可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

看起来对我有用的解决方案是: -

    // Paginate //
    Employees = Employees.Take(11).Skip(0);

更改我的跳过以返回第0行rowIndex。我想我之前的理解是,跳过实际上是在处理分页...我仍然不确定它,但这解决了我的问题。

答案 1 :(得分:0)

出现此问题是因为您没有存储当前索引。您的解决方案取决于您正在使用的方案。

  • 基于Web的环境:我会要求您将当前页面索引存储在会话状态变量中,或将其作为查询参数传递。在您的代码中,您可以通过检索值来发送结果。

  • 表单或商店应用:最好在后面的代码中创建一个全局变量来存储LINQ查询的结果。将值存储在全局变量中将确保您不需要反复迭代。然后,您可以根据用户的查询获取整个结果的子部分。