【问题标题】:Binding LinqToSql tables to Repeater将 LinqToSql 表绑定到中继器
【发布时间】:2011-01-11 09:49:24
【问题描述】:

我有一个从 DB 中检索数据的类。

[Table(Name = "Ilanlar")]
public class Ilan
{

    [Column(Name="ilan_id" ,IsPrimaryKey = true)]
    public int M_ilan_id;

    [Column(Name="refVerenUser_id")]
    public int M_refVerenUser_id;
}

我正在通过上面的类绑定来自 db 的数据。

    private void ItemsGet()
    {
        PagedDataSource objPds = new PagedDataSource();
        objPds.DataSource =  f_IlanlariGetir(CurrentPage);
        objPds.AllowPaging = true;
        objPds.PageSize = 3;

        objPds.CurrentPageIndex = CurrentPage;

        rptIlanlar.DataSource = objPds;  //rptIlanlar = asp:Repeater
        rptIlanlar.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ItemsGet();
    }

    private System.Collections.IEnumerable f_IlanlariGetir(int p)
    {
        Context context = new Context(DAO.dbYaban.ConnectionString);

        return context.GetTable<Ilan>();
    }

但结果是 IEnumerable 但我需要像 DataSet 这样的东西。 我收到此错误:

我找到了关于这个错误的很好的解释,它是:

底层 DataSource 必须支持 ICollection 接口 命令网格执行自动分页。 ICollection 需要一个 类来实现 Count 属性。 ArrayList 和 DataView 都有 支持接口,因此您可以将它们用作数据源。其他类仅支持 IEnumerable 接口。这让他们 用作数据源但不用作分页数据源。 SqlDataReader 就是此类的一个示例。 Reference

但我需要将转发器与 linq 的结果绑定到 sql 表。我该怎么办?

【问题讨论】:

    标签: c# asp.net ienumerable repeater icollection


    【解决方案1】:

    与其直接绑定查询,不如试试:

    return context.GetTable<Ilan>().ToList();
    

    【讨论】:

      猜你喜欢
      • 2016-08-19
      • 2019-02-26
      • 2011-01-17
      • 1970-01-01
      • 2017-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多