This is a migrated thread and some comments may be shown as answers.

Dynamic Linq throws invalid cast exception

4 Answers 121 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Pete
Top achievements
Rank 1
Pete asked on 23 Jun 2011, 03:25 AM
In my RadGridView I'm getting an invalid cast exception from CalculateAggregates when my aggregate function defined below is invoked:

var totalColumnAggregate = new AggregateFunction<DataRow, int?>
{
    AggregationExpression = target => target
        .AsQueryable()
        .Select("new(it[\"Foo\"] AS Foo)")
        .Cast<int?>()
        .Sum()
};

Any suggestions as to why?

Also, on another note, would the following be valid? (I'm trying to build this Select expression on the fly using Dynamic Linq Library)

.Select("new(it[\"Foo\"] AS Foo) + new(it[\"Bar\"] AS Bar) + new(it[\"FooBar\"] AS FooBar)")

4 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 23 Jun 2011, 11:51 AM
Hi Peter,

Could you please send us a small sample project demonstrating this behavior?

Thanks in advance.

Best wishes,
Ross
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Pete
Top achievements
Rank 1
answered on 23 Jun 2011, 12:10 PM
Hi Ross;

This sample will demonstrate the behavior I'm seeing.

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Linq.Dynamic;
 
namespace DynamicLINQ
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable table = new DataTable();
            DataColumn a = new DataColumn();
            DataColumn b = new DataColumn();
            DataColumn c = new DataColumn();
            a.DataType = typeof(int);
            a.ColumnName = "A";
            b.DataType = typeof(int);
            b.ColumnName = "B";
            c.DataType = typeof(int);
            c.ColumnName = "C";
 
            table.Columns.Add(a);
            table.Columns.Add(b);
            table.Columns.Add(c);
 
            DataRow row = table.NewRow();
            row["A"] = 1;
            row["B"] = 2;
            row["C"] = 3;
 
            table.Rows.Add(row);
 
            var _source = row.Table.AsEnumerable().AsQueryable();
 
 
            //var results = _source.Select(i => i.Field<int>("A") + i.Field<int>("B") + i.Field<int>("C"))
            //    .Sum();
 
 
            // dynamic
            var _dynamicResults = _source.Select("new(it[\"A\"] AS A)").Cast<int>().Sum();
 
            Console.WriteLine(_dynamicResults.ToString());
            Console.ReadLine();
 
        }
    }
}
0
Rossen Hristov
Telerik team
answered on 23 Jun 2011, 12:37 PM
Hello Peter,

Unfortunately, I can not see any Telerik controls involved in your sample code.

Does your code work with the standard toolkit DataGrid or with a standard ListBox?

In case you are having problems with RadGridView, could you please prepare a runnable sample project that demonstrates the exact problem you are having with RadGridView and then send it to me.

I will debug it to see what is going on.

Thanks in advance.

Greetings,
Ross
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Pete
Top achievements
Rank 1
answered on 23 Jun 2011, 01:57 PM
Hello again Ross;

Thanks for the feedback. It looks like the issue is in the Dynamic Linq Library. When I have it sorted out I'll post the answer/solution for others who may have the same issue.

Thanks again for your timely replies.
Tags
GridView
Asked by
Pete
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Pete
Top achievements
Rank 1
Share this question
or