Linq查询使用DataTable进行分页

时间:2021-07-01 21:20:34

I have a Linq query that I copy to a DataTable which is then used to populate a gridview. I am using a group by "key" and "count" which I evaluate in the aspx page for a master/detail gridview with a repeater.

我有一个Linq查询,我将其复制到DataTable,然后用于填充gridview。我正在使用“key”和“count”组,我在aspx页面中评估了带有转发器的主/详细网格视图。

The problem that I am encountering is that the gridview datasource and bind to the datatable is not presenting me with any additional pages that are part of the data. My query is:

我遇到的问题是gridview数据源和绑定到数据表没有向我提供作为数据一部分的任何其他页面。我的查询是:

// Using Linq generate the query command from the DataTable
var query = from c in dtDataTable_GridView.AsEnumerable()
group c by c.Field<string>("CLIN") into g
select new
{
    Key = g.Key,
    Count = g.Count(),
    Items = from i in g                                      
        select new
        {
            CLIN = i.Field<string>("CLIN"),
            SLIN = i.Field<string>("SLIN"),
            ACRN = i.Field<string>("ACRN"),
            CLINType = i.Field<string>("CLINType"),
            Option = i.Field<string>("Option"),
            Unit = i.Field<string>("Unit")
        }
};

// Use extension methods to create new DataTable from query
dtTaskOrderTable = query.CopyToDataTable();

// Set the datasource
gridview1.DataSource = dtTaskOrderTable;

// Bind to the GridView
gridview1.DataBind();  

If I use the original datatable (dtDataTable_GridView) directly I have paging but once I do the Linq Query and copy it back to a new datatable (dtTaskOrderTable) I lose the paging feature.

如果我直接使用原始数据表(dtDataTable_GridView)我有分页但是一旦我执行了Linq查询并将其复制回新的数据表(dtTaskOrderTable),我就失去了分页功能。

Also how do I get a value from a column name ("Option" for instance) if it is part of "Items"?

如果它是“项目”的一部分,如何从列名称(例如“选项”)中获取值?

Any help would be appreciated. Thanks, ChrisB

任何帮助,将不胜感激。谢谢,ChrisB

1 个解决方案

#1


Please disregard the previous answer I will delete it

请忽略之前的答案,我将删除它

It requires ICollection interface for paging. Neither IEnumerable nore IQuerable will not work

它需要ICollection接口进行分页。 IEnumerable nore IQuerable都不会起作用

List<(Of <(T>)>) will work as Lists implement Icollection interface

List <(Of <(T>)>)将作为Lists实现Icollection接口

So you need

所以你需要

gridview1.DataSource = dtTaskOrderTable.ToList();

#1


Please disregard the previous answer I will delete it

请忽略之前的答案,我将删除它

It requires ICollection interface for paging. Neither IEnumerable nore IQuerable will not work

它需要ICollection接口进行分页。 IEnumerable nore IQuerable都不会起作用

List<(Of <(T>)>) will work as Lists implement Icollection interface

List <(Of <(T>)>)将作为Lists实现Icollection接口

So you need

所以你需要

gridview1.DataSource = dtTaskOrderTable.ToList();