none
EF and Linq - how to modulize a query (Extension method?) for checking a Flag common to several classes? RRS feed

  • Question

  • If I have EF entity classes A and B that both have a bool property named "Active" on them, I made partial classes of A and B implementing an IHasActiveFlag interface (must have an Active prop) to link them together that way.. but how do I hook up a method to do this:

    context.A.ActiveItems();//filters
    or context.B.ActiveItems();//filters
    or context.C.ActiveItems();//Doesn't filter, because no Active property

    I tried defining an ActiveItems method as something like, but that dosen't compile: 

            
            public static IQueryable<EntityObject> ActiveItems(this ObjectSet<EntityObject> set)
            {
                if (set is IHasActiveFlag)
                {
                    return set.Where(i => ((IHasActiveFlag)i).Active == true);
     
                }
                return set;
            }


    Sunday, November 4, 2012 10:37 PM

All replies

  • Hi ryanb2009,

    Welcome to MSDN Forum.

    Are you using database first or code first? Based on the parameter type you passed to the extension method, I think it is not code first, but you have an interface and the two entites implement the interface, it looks like code first. Furthermore, could you please post the exception which thrown out in compile time?

    Best Regards


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

    Tuesday, November 6, 2012 2:47 AM
    Moderator
  • It's not code first- the model was generated from the db. I added the interface with partial classes, I was saying.

    Actually there is no compile error surprisingly - that was a property typo.

    The first issue is that the method ActiveItems isn't visible in the intellisense list after something that's of type: ObjectSet<a>
    ie:  Context.a.

    it doesn't come up know why??

    Also, in the ActiveItems, I would have to check whether or not EntityObject is IHasActiveFlag - but i'm not sure how to do that.

    I could try: set.EntitySet.Name is IHasActiveFlag
    but that wouldn't work because Name is a string

    Tuesday, November 6, 2012 10:42 PM
  • Does something like the following work to get it into intellisense?

    public static IQueryable<T> ActiveItems<T>(this ObjectSet<T> set) where T : class
            {
                if (set is IHasActiveFlag)
                {
                    return set.Where(i => ((IHasActiveFlag)i).Active == true);
    
                }
                return set;
            }

    Even if it does I'm not sure that this is really worth the effort and complexity. Do you have some examples of why you think having this method helps make queries easier to understand?


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.



    Friday, November 9, 2012 12:50 AM
    Moderator