【问题标题】:Setting where clause and parameters of linqdatasource like a Linq query像 Linq 查询一样设置 linqdatasource 的 where 子句和参数
【发布时间】:2013-05-14 08:52:26
【问题描述】:

我有一个列表视图并将其绑定在代码隐藏中,来自一个选择相关关键字的 linq 查询

MyDataContext dcxt = new MyDataContext();
string x = "searchword"; // i get x from user
var query = from ak in dcxt.WordIndexes
            where ak.Word == x
            from a in ak.Keywords
            where a.Comps.Approved &&
            a.Comps.Active &&
            a.Approved 
            orderby a.ETbm descending
            select a;

ListView1.DataSource = query;
ListView1.DataBind();

我在WordIndexes -> Keywords 之间有一对多的关系, Comps -> Keyswords 之间也是一对多的。

现在我想使用 linqdatasource,所以我可以让 linqdatasource 处理分页。但我不能像这个 linq 查询那样设置 where 子句。

<asp:LinqDataSource ID="lds2" runat="server" 
    ContextTypeName="MyDataContext" EntityTypeName="" 
    OrderBy="" TableName="Keywords"
    Where="">
</asp:LinqDataSource>

我尝试使用 datapager(没有 linqdatasource)进行分页,但如果结果包含很多项目,它会非常慢。我之前使用 linqdatasource 进行更简单的查询,但这对我来说有点复杂,因为表之间的关系而且我不知道从哪里开始构建 where 子句。有什么帮助吗?

更新:

我最终使用 LinqDataSourceSelectEvent 和答案中建议的 DataPager

protected void linqDS_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    var query = from ak in dcxt.WordIndexes
                where ak.Word == x
                from a in ak.Keywords
                where a.Comps.Approved &&
                a.Comps.Active &&
                a.Approved 
                orderby a.ETbm descending
                select a;

    e.Arguments.TotalRowCount = query.Count();
    e.Result = query.Skip(DataPager1.StartRowIndex).Take(DataPager1.PageSize);
}

【问题讨论】:

    标签: c# asp.net linq listview linqdatasource


    【解决方案1】:

    您可以使用 LinqDataSource_Selecting 事件并将查询传递给

    【讨论】:

      猜你喜欢
      • 2010-12-28
      • 2020-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多