Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 使用实体框架扩展过滤器查询_Entity Framework - Fatal编程技术网

Entity framework 使用实体框架扩展过滤器查询

Entity framework 使用实体框架扩展过滤器查询,entity-framework,Entity Framework,我的testdatabase中有3个表,Customers CustomersUsergroups Usergroups 对于客户,我有一个返回所有用户组的方法,如下所示: public IQueryable<Usergroup> GetUsergroups() { return from ug in _entities.UsergroupSet.Include("Customer") select ug;

我的testdatabase中有3个表,Customers CustomersUsergroups Usergroups

对于客户,我有一个返回所有用户组的方法,如下所示:

        public IQueryable<Usergroup> GetUsergroups()
    {
        return from ug in _entities.UsergroupSet.Include("Customer")
               select ug;
    }
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;
     }
它工作得很好,但现在的问题是我想扩展它,这样我也可以通过CustomerID进行过滤,有点像:

public static IQueryable<Customer> ByCustomerID(this IQueryable<Customer> qry, int customerID)
    {
        return from c in qry
               where c.CustomerID == customerID
               select c;
    }
仅仅使用“Include”就可以做到这一点吗?或者这不是一个简单的方法来解决这个问题,因为它们是不同的类

提前谢谢


/M

我不确定我是否理解了您描述的问题,但您不能使用CustomerID方法是正常的,因为您在
IQueryable
上定义了扩展,而不是在
IQueryable

如果您想在
IQueryable
上定义它,并且如果您想将用户组分配给一个客户,可以这样做:

        public IQueryable<Usergroup> GetUsergroups()
    {
        return from ug in _entities.UsergroupSet.Include("Customer")
               select ug;
    }
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;
     }
public static IQueryable ByCustomerID(此IQueryable qry,int customerID)
{
在qry中从ug返回
来自ug.Customer中的c
其中c.CustomerID==CustomerID
选择ug;
}
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;
     }