0

我想让继承的类有机会为我的查询提供 where 子句。这可能吗?

    protected IQueryable<EntityResult> GetEntities(ETBDataContext pContext)
    {
        return from e in pContext.Entities
               where e.StatusCode == "Published"

               //is there a way to add a dynamic where clause here?
               //I would like to ask the inherited class for it's input:

               && e.OtherColumn == "OtherValue" // <-- like GetWhere(e)?

               //without having to select the column

               orderby e.PublishDate descending

               select new EntityResult
               {
                   Name = e.Name,
                   Link = e.Link
               };
    }

提前致谢!!!!!!!!!!

4

4 回答 4

3

由于您返回 IQueryable,因此仅在使用资源时才会执行查询。因此,只要它保持 IQueryable 状态,它就会只保留查询。

有了这些知识,您可以简单地在返回 IQueryable 的函数上应用 where

像这样:

myObject.GetEntities(myContextObject).Where(x => x.id == 5);

由于您提到的 OtherCOlumn 不存在,您可以将默认查询更改为:

 protected IQueryable<EntityResult> GetEntities(ETBDataContext pContext)
{
    return (from e in pContext.Entities
           where e.StatusCode == "Published"

           //is there a way to add a dynamic where clause here?
           //I would like to ask the inherited class for it's input:

           && e.OtherColumn == "OtherValue" // <-- like GetWhere(e)?

           //without having to select the column

           orderby e.PublishDate descending

           select e).FirstOrDefault();
}

然后在您的扩展位置中进行选择。因为只要返回类型保持 IQueryable 它就保持查询,这不会使其变慢

于 2012-11-21T08:13:30.833 回答
0

Where 子句是一种扩展方法,因此您不能覆盖它,但您可以将动态构造的表达式树传递给 where 子句并产生所需的结果。看看这篇 MSDN 文章:

http://msdn.microsoft.com/en-us/library/bb882637.aspx

于 2012-11-21T08:13:19.700 回答
0

我不知道如何用 Linq 表示法写这个,因为我总是喜欢使用 Linq 扩展方法,但它看起来像这样:

protected IQueryable<EntityResult> GetEntities(ETBDataContext pContext)
{
    var q = pContext.Entities
        .Where(e => e.StatusCode == "Published");
    q = q.AddWhereCondition(q)
        .OrderByDescending(e => e.PublishDate)
        .Select(e => new EntityResult
           {
               Name = e.Name,
               Link = e.Link
           });
}

protected virtual IQueryable<Entity> AddWhereCondition(IQueryable<Entity> query)
{
    return query.Where(e => e.OtherColumn == "OtherValue");
}

或者,通过将 Where() 条件提取为 Linq 表达式:

protected IQueryable<EntityResult> GetEntities(ETBDataContext pContext)
{
    var q = pContext.Entities
        .Where(e => e.StatusCode == "Published")
        .Where(e => GetWhereCondition(e))
        .OrderByDescending(e => e.PublishDate)
        .Select(e => new EntityResult
           {
               Name = e.Name,
               Link = e.Link
           });
}

protected virtual Expression<Func<Entity, bool>> GetWhereCondition(Entity e)
{
    return e => e.OtherColumn == "OtherValue";
}
于 2012-11-21T08:22:24.857 回答
0

您可以定义一个可以应用额外 where 条件的虚拟方法:

protected IQueryable<EntityResult> GetEntities(ETBDataContext pContext)
{
    IQueryable<Entity> query = pContext.Entities
       .Where(e => e.StatusCode == "Published");

    query = ApplyWhereClause(query);

    return from e in query
           orderby e.PublishDate descending
           select new EntityResult
               {
                   Name = e.Name,
                   Link = e.Link
               };
    }

protected virtual IQueryable<Entity> ApplyWhereClause(IQueryable<Entity> entities)
{

}

在您的派生类中,您将执行以下操作:

protected override IQueryable<Entity> ApplyWhereClause(IQueryable<Entity> entities)
{
    return entities.Where(/* insert your extra where clause here */);
}
于 2012-11-21T08:24:29.400 回答