ChrisB
ChrisB

Reputation:

Linq query on 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.

Upvotes: 3

Views: 1685

Answers (2)

dahlbyk
dahlbyk

Reputation: 77600

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

Upvotes: 3

ak3nat0n
ak3nat0n

Reputation: 6298

Is it possible that the Eval("SLIN") you wrote is supposed to be Eval("CLIN") ? just a thought. Otherwise,i used the same code before and it works.

Upvotes: 0

Related Questions