Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.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
C# 是否可以将查询规则应用于LINQ到SQL关联列表?_C#_Sql_Linq - Fatal编程技术网

C# 是否可以将查询规则应用于LINQ到SQL关联列表?

C# 是否可以将查询规则应用于LINQ到SQL关联列表?,c#,sql,linq,C#,Sql,Linq,我们所有的表中都有标准的db列,其中一个是布尔“Active”。因此,我们没有实际删除数据,而是将活动属性设置为false 当我们使用关联属性时,会出现问题,如以下关系: User.Company.Name(Users.CompanyId->companys.Id) 如果相关公司被标记为Active=false,当Active属性的值为false时,是否可以在我的数据访问层中编写一个通用规则来排除列表中的项目? 目前,我们只有一个普通的Linq-to-Sql配置。我不熟悉Linq-to-Sql(

我们所有的表中都有标准的db列,其中一个是布尔“Active”。因此,我们没有实际删除数据,而是将活动属性设置为false

当我们使用关联属性时,会出现问题,如以下关系:

User.Company.Name(Users.CompanyId->companys.Id)

如果相关公司被标记为Active=false,当Active属性的值为false时,是否可以在我的数据访问层中编写一个通用规则来排除列表中的项目?


目前,我们只有一个普通的Linq-to-Sql配置。

我不熟悉Linq-to-Sql(更多的是EF),但我想如果您的所有具有该属性的类都共享一个公共接口,这将得到解决,例如:

public interface IHasActive
{
    bool Active { get; set; }
}
然后可以实现一个扩展方法:

public static class IHasActiveExtensions
{
    public static IQueryable<T> ExcludeInactive<T>(this IQueryable<T> query)
        where T : IHasActive
    {
        return query.Where(m => m.Active);
    }
}
公共静态类IHasActiveExtensions
{
公共静态IQueryable ExcludeInactive(此IQueryable查询)
其中T:IHasActive
{
返回query.Where(m=>m.Active);
}
}

我不熟悉Linq to SQL(更多的是EF),但我认为如果所有具有该属性的类都共享一个公共接口,就会解决这个问题,例如:

public interface IHasActive
{
    bool Active { get; set; }
}
然后可以实现一个扩展方法:

public static class IHasActiveExtensions
{
    public static IQueryable<T> ExcludeInactive<T>(this IQueryable<T> query)
        where T : IHasActive
    {
        return query.Where(m => m.Active);
    }
}
公共静态类IHasActiveExtensions
{
公共静态IQueryable ExcludeInactive(此IQueryable查询)
其中T:IHasActive
{
返回query.Where(m=>m.Active);
}
}