IQueryable where子句在继承的类中重写

时间:2012-11-21 08:09:46

标签: c# linq entity-framework

我想为继承的类提供一个向查询提供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 个答案:

答案 0 :(得分:3)

由于您返回IQueryable,因此只有在使用资源时才会执行查询。因此,只要它保持IQueryable,它将只是查询。

有了这些知识,你可以简单地在你的函数中应用返回IQueryable的位置

像这样:

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

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

 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,它就会保持查询,这不会使它变慢

答案 1 :(得分:0)

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

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

答案 2 :(得分: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";
}

答案 3 :(得分: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 */);
}