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
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
- 편집됨 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:17Where 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!
- 답변으로 제안됨 Doraemon_3 2012년 8월 27일 월요일 오전 1:17
- 답변으로 표시됨 Allen Li - AI3Microsoft Contingent Staff, Moderator 2012년 8월 30일 목요일 오전 5:24
-
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

