Is it possible to implement a extension method to ObjectContext Class which filters the database rows and returns the ObjectSet<TEntity>

답변됨 Is it possible to implement a extension method to ObjectContext Class which filters the database rows and returns the ObjectSet<TEntity>

  • 2012년 8월 22일 수요일 오전 9:45
     
     

    Hi,

         Please refer to the following link 

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/9ae8d33b-2684-46cd-a77f-ed4a6432cce0

    This question is related to the above link. I m trying different approach for the issue mentioned in the link. I will explain the idea i have in my mind. I m planning to write a extension method to ObjectContext class which is similar to overloaded methods it has

    1.public ObjectSet<TEntity> CreateObjectSet<TEntity>() where TEntity : class

    2.public ObjectSet<TEntity> CreateObjectSet<TEntity>(string entitySetName) where TEntity : class

    Which returns the rows from database of type ObjectSet<TEntity>. If you want filter we can add where class to it which returns ObjectQuery<TEntity>. The Extension method filters the rows and returns them as ObjectSet<TEntity>

    Extension Method - 

    public static ObjectSet<TEntity> CreateFilteredObjectSets<TEntity>(this ObjectContext context, string entitySetName) where TEntity : class
            {

    //Actually ObjectSet already has method Where()

    //Which it inherits from ObjectQuery, so its return type is ObjectQuery<TEntity>

    //But What I m looking for where method which filters objects and returns objects of type ObjectSet<TEntity> instead of ObjectQuery<TEntity>

    return base.CreateObjectSet<TEntity>("EntityName").Where(condition);

    }

    Extension method above is Psuedocode I have in my mind. Let me know this is possible first. If so how to implement this??

    Thanks

    Murali


    • 편집됨 SmaaartCoder 2012년 8월 22일 수요일 오전 9:46
    •  

모든 응답

  • 2012년 8월 22일 수요일 오전 10:20
     
     

    OR

    Can I write a Extension method where() which returns ObjectSet<TEntity> to the ObjectSet Class??

  • 2012년 8월 23일 목요일 오전 2:17
     
     
    Where statement will always return a ObjectQuery<T> type or a IQueryable<T> type, so a method return a filterd ObjectSet is not possible.

    Go go Doraemon!

  • 2012년 8월 23일 목요일 오전 6:58
     
     

    Ok I will put my question in different way. Extension method which filters the entity rows and returns ObjectSet<TEntity>

    public static ObjectSet<TEntity> CreateFilteredObjectSets<TEntity>(this ObjectContext context, string entitySetName) where TEntity : class
            {

    //Actually ObjectSet already has method Where()

    //Which it inherits from ObjectQuery, so its return type is ObjectQuery<TEntity>

    //But What I m looking for is method which filters objects and returns objects of type ObjectSet<TEntity> instead of ObjectQuery<TEntity>

    }

    I am not talking about the Where method here. Just some extension method which could solve the above issue using the some other API's exposed by Entity Framework.

  • 2012년 8월 23일 목요일 오전 7:46
     
     답변됨
    Not possible, if you do a filter, the return type will never be ObjectSet type. If you could clarify what the apis you want to use only exposed for ObjectSet, maybe I can help you to think if there's another way.

    Go go Doraemon!

  • 2012년 8월 27일 월요일 오전 1:45
    중재자
     
     

    Hi SmaaartCoder,

    I agree with @Dorado999, if you do a filter in the method, the return type will not be ObjectSet.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us