大佬教程收集整理的这篇文章主要介绍了LINQ to SQL 过滤子集合,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在为这个查询苦苦挣扎,我想我错过了一些东西。 我有两个自动生成的 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;
}
目前我正在加载所有匹配的监管者组织视图,并对监管者进行过滤。
List<RegulatorOrganizationVIEw> regOrgs = boatDataContext.RegulatorOrganizationVIEw
.Where(r => r.RegulatorsVIEws.Any(ar => ar.regulatorname.toupper().Contains(filterText.toupper()))
|| r.regulatorname.toupper().Contains(filterText.toupper())
.ToList();
但是这样我加载冗余 调节器只是为了稍后过滤掉它们。 如何重建此查询以仅从启动器加载匹配的调节器?
它尝试使用 SELEct() 分配 Regulators 的regulatorOrgnization 过滤器列表。
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();
但我收到异常:message=“不允许在查询中显式构造实体类型“RegulatorsOrganizationVIEw”。”
看起来像过滤的 Include() 将是一个选项(就像在 EF 中一样),但我找不到将它与 linq To sql 一起使用的方法。 有什么想法吗?
在 LINQ-to-SQL 中,这样做有点混乱且不直观。您必须使用 DataLoadoptions:
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());
所以这就是说:当加载 RegulatorOrganizationViews
时,当它们的 regulatorsViews
相关联时,使它们满足给定的条件。
然后它说:当加载RegulatorOrganizationViews
时,也加载他们的regulatorsViews
。
后者类似于实体框架中的 Include
。前者使它的行为类似于过滤的 Include
,或者更接近于全局查询过滤器。
为简洁起见,我删除了 ToUpper
调用,但如果数据库排序规则不区分大小写,则不需要它们。
以上是大佬教程为你收集整理的LINQ to SQL 过滤子集合全部内容,希望文章能够帮你解决LINQ to SQL 过滤子集合所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。