迭代由Linqdatasource返回的对象 [英] Iterating over objects returned by Linqdatasource

查看:252
本文介绍了迭代由Linqdatasource返回的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView,过滤和分页(一次10个),绑定到一个Linqdatasource。所有这些工作。



但是,如何获取LinqDataSource中检索到的所有数据的ID,在完成所有行的检索后?



我有这个方法,e.Result是一个包含此网格列表的对象数据类型

  protected void LinqDataSource_Selected(object sender,LinqDataSourceStatusEventArgs e)//事件在数据检索完成后触发。 
{
List< int> ids = new List< int>();
if(e.TotalRowCount> 0)
{
for(int idx = 0; idx< e.TotalRowCount; idx ++)
{
Foo foo = (Foo)(e.Result [idx]); //错误不能应用索引到类型对象的表达式
ids.Add(foo.Id);
}
}
}

我的错误是迭代

  protected void LinqDataSource_Selected(object sender,LinqDataSourceStatusEventArgs e)//事件在数据检索完成后触发。 
{
List< int> ids = new List< int>();
if(e.TotalRowCount> 0)
{
List< Foo> fooList =((IEnumerable)e.Result).ToList();
for(int idx = 0; idx< fooList.Count; idx ++)
{
Foo foo = fooList [idx];
ids.Add(foo.Id);
}
}
}

  protected void LinqDataSource_Selected(object sender,LinqDataSourceStatusEventArgs e)//事件在数据检索完成后触发。 
{
List< int> ids = new List< int>();
if(e.TotalRowCount> 0)
{
foreach(Foo foo in(IEnumerable)e.Result)
{
ids.Add(foo.Id );
}
}
}

的过滤视图,e.Result将是一个匿名类型的IEnumerable,因此获取信息很可能需要使用IQueryable和viewmodel对象。


I have a GridView, with filtering and pagination (10 at a time), bound to a Linqdatasource. All this works.

But how can I get the Ids of all the data retrieved in the LinqDataSource after it has finished retrieval of all rows?

I have this method, and e.Result is an object data type containing List for this grid

protected void LinqDataSource_Selected(object sender, LinqDataSourceStatusEventArgs e)  // event fires after data retrieval complete.
{
    List<int> ids = new List<int>();
    if (e.TotalRowCount > 0)
    {
        for (int idx = 0; idx < e.TotalRowCount; idx++)
        {
            Foo foo = (Foo)(e.Result[idx]);  // error cannot apply indexing to expression of type object
            ids.Add(foo.Id);
        }
    }
}

My error is iterating over an object, how can this be done?

解决方案

You can either do:

protected void LinqDataSource_Selected(object sender, LinqDataSourceStatusEventArgs e)  // event fires after data retrieval complete.
{
    List<int> ids = new List<int>();
    if (e.TotalRowCount > 0)
    {
        List<Foo> fooList = ((IEnumerable)e.Result).ToList();
        for (int idx = 0; idx < fooList.Count; idx++)
        {
            Foo foo = fooList[idx];
            ids.Add(foo.Id);
        }
    }
}

Or

protected void LinqDataSource_Selected(object sender, LinqDataSourceStatusEventArgs e)  // event fires after data retrieval complete.
{
    List<int> ids = new List<int>();
    if (e.TotalRowCount > 0)
    {
        foreach(Foo foo in (IEnumerable)e.Result)
        {
            ids.Add(foo.Id);
        }
    }
}

If your Selected is a result of a filtered view, e.Result will be an IEnumerable of anonymous type, so getting the information will most likely require using an IQueryable and a viewmodel object.

这篇关于迭代由Linqdatasource返回的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆