Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq query on DataTable

Tags:

linq

datatable

I have a DataTable that is filled from a stored procedure. I am doing a query on the DataTable using a groupby so that I can implement a ListView within a ListView. (Matt Berseth - Building a Grouping Grid with the ASP.NET 3.5 LinqDataSource and ListView Controls)

My Query in my codebehind:

var query = from c in dtTaskOrder.AsEnumerable() 
        group c by c.Field<string>("CLIN") into g
        select new
        {
            Key = g.Key,
            Count = g.Count(),
            Items = g
        };

listv.DataSource = query.ToList();
listv.DataBind();

In my aspx file I am trying to Eval on the Items and the subsequent columns:

<asp:ListView ID="lv1" ... DataSource='<%# Eval("Items") %>'>

   <td><%# Eval("SLIN") %></td> // column name
   <td><%# Eval("ACRN") %></td> // column name
   <td><%# Eval("Status") %></td> // last column name

The HttpException was unhandled by user code - when it tries to Eval on the above column names.

How can I formulate the above query so that "Items" is "Typed" and I can use the column names.

Thanks for your help.


1 Answers

If it's not just the typo, you could use a subquery on g to build a collection of strongly-typed objects:

var query = from c in dtTaskOrder.AsEnumerable()
            group c by c.Field("CLIN") into g
            select new
            {
                Key = g.Key,
                Count = g.Count(),
                Items = from i in g
                        select new { CLIN = i.Field("CLIN"),
                                     ACRN = i.Field("ACRN"),
                                     Status = i.Field("Status") }
            };
like image 147
dahlbyk Avatar answered Dec 11 '25 14:12

dahlbyk