如何将网格数据复制到对象

时间:2012-11-12 07:30:26

标签: linq casting grid

我正在尝试将网格数据复制到一个对象。

代码:

object obj = GrdReport.ItemsSource;    
PrepareDataForStackedChart1(obj);

该功能定义为

private void PrepareDataForStackedChart1(object categoies)           
{              
    var Salespersons = (from cat in categoies    
                    select cat.Salesperson);    
}

我收到错误:

  

无法从'lambda expression'转换为'System.Linq.Expressions.LambdaExpression'

有谁能告诉我如何在linq查询中访问该对象?

1 个答案:

答案 0 :(得分:0)

修改:刚好了解到您使用的是ItemSource属性需要System.Object而不是IEnumerable的第三方控件(from your duplicate question) 。在这种情况下,要么将对象(ItemSource)强制转换回原始类型,要么维护用于将控件绑定到某处的原始集合,并将该集合传递给PrepareDataForStackedChart1方法。

有些猜测工作:

1)

object obj = GrdReport.ItemsSource;    
PrepareDataForStackedChart1((IEnumerable<Category>)obj);

private void PrepareDataForStackedChart1(IEnumerable<Category> categories)           
{              
    var Salespersons = (from cat in categories    
                    select cat.Salesperson);    
}

2)当你做

GrdReport.ItemsSource = GetCategories();

复制备份,如下所示:

categories = GetCategories(); //categories is defined in proper scope.
GrdReport.ItemsSource = categories;

以后你做;

PrepareDataForStackedChart1(categories);

private void PrepareDataForStackedChart1(IEnumerable<Category> categories)           
{              
    var Salespersons = (from cat in categories    
                    select cat.Salesperson);    
}

3)或者您可能会从GridView上定义的ItemsRows属性(可能)获得该集合。祝你好运..


预编辑

错误是因为您无法枚举普通对象。它必须是可枚举的。为什么要将对象值传递给PrepareDataForStackedChart1方法?相反,你不能通过IEnumerable本身?尝试

var categories = GrdReport.ItemsSource.OfType<Category>();    
PrepareDataForStackedChart1(categories);

private void PrepareDataForStackedChart1(IEnumerable<Category> categories)           
{              
    var Salespersons = (from cat in categories    
                        select cat.Salesperson);    
}

我假设您有父类Category