views:

188

answers:

1

Hello

I have 3 tables in my testdatabase, Customers <-> CustomersUsergroups <-> Usergroups

For customers I have a method that returns all Usergroups like this:

        public IQueryable<Usergroup> GetUsergroups()
    {
        return from ug in _entities.UsergroupSet.Include("Customer")
               select ug;
    }

And to that I have a "filter-class" for class Usergroup

        public static IQueryable<Usergroup> ByUsergroupID(this IQueryable<Usergroup> qry, int usergroupID)
    {
        return from ug in qry
               where ug.UsergroupID == usergroupID
               select ug;
    }

so when I type:

return _repository.GetUsergroups().ByUsergroupID(usergroupID);

it works great, but the problem now is that I would like to extend so I can filter by CustomerID aswell, sort of like:

public static IQueryable<Customer> ByCustomerID(this IQueryable<Customer> qry, int customerID)
    {
        return from c in qry
               where c.CustomerID == customerID
               select c;
    }

so I can use it like:

return _repository.GetUsergroups().ByCustomerID(customerID);

is that possible to do with just using "Include"? or isnt it an easy way to fix that since they are different classes?

Thanks in advance

/M

+2  A: 

Hallo, I'm not sure if I understand problem the way you described, but it is normal that you can't use method ByCustomerID because you defined extension on IQueriable not on IQueriable.

If you want to define it on IQueriable and if you intended to get UserGroups assigned to one Customer that can be done like this:

public static IQueryable<UserGroup> ByCustomerID(this IQueryable<UserGroup> qry, int customerID)
     {
         return from ug in qry
                from c in ug.Customer
                where c.CustomerID == customerID
                select ug;
     }
Misha N.
awesome, thx alot :)
molgan
no problemo, glad that I could help...
Misha N.