2013-05-14 65 views
1

我有一个列表视图,并从该选择LINQ查询在代码隐藏结合它相关关键词设置where子句和使用LinqDataSource的参数,如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); 
} 

回答

0

可以使用LinqDataSource_Selecting事件和传递查询