C# LINQ到SQL筛选器子集合

C# LINQ到SQL筛选器子集合,c#,linq-to-sql,C#,Linq To Sql,我在苦苦思索这个问题,我想我遗漏了什么。 我有两个自动生成的dbml模型 public partial class RegulatorsOrganizationView { private int regulatorOrgId; private string regulatorOrgName; private EntitySet<RegulatorsView> regulatorsViews; } pu

我在苦苦思索这个问题,我想我遗漏了什么。 我有两个自动生成的dbml模型

    public partial class RegulatorsOrganizationView
    {
        private int regulatorOrgId;
        private string regulatorOrgName;
        private EntitySet<RegulatorsView> regulatorsViews;
    }

    public partial class RegulatorsView
    {
        private int regulatorId;
        private string regulatorName;
    }
但我得到了一个例外:Message=“不允许在查询中显式构造实体类型'RegulatorOrganizationView'。

看起来过滤Include()将是一个选项(如在EF中),但我找不到一种方法将其与Linq to SQL一起使用。
有什么想法吗?

在LINQ to SQL中,这样做有点混乱,而且不直观。您必须使用:

这说明:当加载
regulatorororganizationview
时,当它们的
regulatorview
关联时,使它们满足给定条件

然后它会说:当加载
监管机构组织视图
时,也加载他们的
监管机构视图

后者类似于实体框架中的
Include
。前者使其行为类似于过滤的
包含
,或者更接近于全局查询过滤器


为了简洁起见,我删除了
ToUpper
调用,但是如果数据库排序规则不区分大小写,则不需要它们。

正如您所说,这确实是混乱的,而且不直观。难怪我挣扎。但是你的解决方案对我很好,非常感谢。
List<RegulatorOrganizationView> regOrgs = boatDataContext.RegulatorOrganizationView
                .Where(r => r.RegulatorsViews.Any(ar => ar.regulatorName.ToUpper().Contains(filterText.ToUpper()))
                || r.regulatorName.ToUpper().Contains(filterText.ToUpper())
                .ToList();

         
            regulatorsOrgs = DataContext.RegulatorOrganizationViews
            .Where(ro => ro.regulatorOrgName.ToUpper().Contains(filterText.ToUpper())
            || ro.RegulatorsViews.Any(r => r.regulatorName.ToUpper().Contains(filterText.ToUpper()))
            .Select(ro => new RegulatorOrganizationView()
            {
                regulatorId = ro.regulatorId,
                regulatorOrgName = ro.regulatorOrgName,

                RegulatorsViews = ro.RegulatorsViews
                         .Where(r => r.regulatorName.ToUpper().Contains(filterText.ToUpper())
                         .Select(r => new RegulatorsView()
                         {
                             regulatorId = r.regulatorId,
                             regulatorName = r.regulatorName,
                         }).ToEntitySet()
            
            }).ToList();
var opt = new DataLoadOptions();

opt.AssociateWith((RegulatorsOrganizationView v) 
    => v.regulatorsViews.Where(ar => ar.regulatorName.Contains(filterText)));

opt.LoadWith((RegulatorsOrganizationView v) => => v.regulatorsViews);

DataContext.LoadOptions = opt;

var result = DataContext.RegulatorOrganizationViews
            .Where(ro => ro.regulatorOrgName.Contains(filterText) 
                      && ro.regulatorsViews.Any());