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 に答える