我可以在 EF Core 中混合使用 LINQ 和 Fluent Syntax 吗?
Can I mix LINQ and Fluent Syntax in EF Core?
我有以下查询工作正常(因为它生成正确的 SQL 命令):
var sites = from sm in this.context.SiteMemberships
join s in this.context.Sites on sm.SiteUid equals s.SiteUid
join sd in this.context.SiteData on s.SiteUid equals sd.SiteUid
join p in this.context.Providers on s.ProviderUid equals p.ProviderUid
join r in this.context.Regions on p.RegionUid equals r.RegionUid
join o in this.context.Offices on s.OfficeUid equals o.OfficeUid
join u in this.context.Users on sm.UserUid equals u.UserUid
where
u.Email == userEmail ||
EF.Functions.Like(s.Classification, "117400-74%")
select new {
s.Field1,
sd.Field2,
p.Field3
};
如您所见,这是一个复杂的查询,尝试使用 Fluent API 非常 很麻烦。但是,我希望 'Where' 子句是可编程的。根据当前用户拥有的特征,他们应该能够根据不同的标准进行搜索。 1 级支持人员只能搜索分类字段的完全匹配项,但 2 级支持人员可以搜索通配符。
有没有办法混合语法。我想要的是这样的:
var (sites = from sm in this.context.SiteMemberships
join s in this.context.Sites on sm.SiteUid equals s.SiteUid
join sd in this.context.SiteData on s.SiteUid equals sd.SiteUid
join p in this.context.Providers on s.ProviderUid equals p.ProviderUid
join r in this.context.Regions on p.RegionUid equals r.RegionUid
join o in this.context.Offices on s.OfficeUid equals o.OfficeUid
join u in this.context.Users on sm.UserUid equals u.UserUid)
.Where(g => this.GetSearchCriteria(g))
.Select(g => g);
您需要在最后一个 join
之后添加 select new { s, sd, p, r, o, u }
var sites = (from sm in this.context.SiteMemberships
join s in this.context.Sites on sm.SiteUid equals s.SiteUid
join sd in this.context.SiteData on s.SiteUid equals sd.SiteUid
join p in this.context.Providers on s.ProviderUid equals p.ProviderUid
join r in this.context.Regions on p.RegionUid equals r.RegionUid
join o in this.context.Offices on s.OfficeUid equals o.OfficeUid
join u in this.context.Users on sm.UserUid equals u.UserUid
select new { s, sd, p, r, o, u })
.Where(g => this.GetSearchCriteria(g))
.Select(g => g);
我有以下查询工作正常(因为它生成正确的 SQL 命令):
var sites = from sm in this.context.SiteMemberships
join s in this.context.Sites on sm.SiteUid equals s.SiteUid
join sd in this.context.SiteData on s.SiteUid equals sd.SiteUid
join p in this.context.Providers on s.ProviderUid equals p.ProviderUid
join r in this.context.Regions on p.RegionUid equals r.RegionUid
join o in this.context.Offices on s.OfficeUid equals o.OfficeUid
join u in this.context.Users on sm.UserUid equals u.UserUid
where
u.Email == userEmail ||
EF.Functions.Like(s.Classification, "117400-74%")
select new {
s.Field1,
sd.Field2,
p.Field3
};
如您所见,这是一个复杂的查询,尝试使用 Fluent API 非常 很麻烦。但是,我希望 'Where' 子句是可编程的。根据当前用户拥有的特征,他们应该能够根据不同的标准进行搜索。 1 级支持人员只能搜索分类字段的完全匹配项,但 2 级支持人员可以搜索通配符。
有没有办法混合语法。我想要的是这样的:
var (sites = from sm in this.context.SiteMemberships
join s in this.context.Sites on sm.SiteUid equals s.SiteUid
join sd in this.context.SiteData on s.SiteUid equals sd.SiteUid
join p in this.context.Providers on s.ProviderUid equals p.ProviderUid
join r in this.context.Regions on p.RegionUid equals r.RegionUid
join o in this.context.Offices on s.OfficeUid equals o.OfficeUid
join u in this.context.Users on sm.UserUid equals u.UserUid)
.Where(g => this.GetSearchCriteria(g))
.Select(g => g);
您需要在最后一个 join
select new { s, sd, p, r, o, u }
var sites = (from sm in this.context.SiteMemberships
join s in this.context.Sites on sm.SiteUid equals s.SiteUid
join sd in this.context.SiteData on s.SiteUid equals sd.SiteUid
join p in this.context.Providers on s.ProviderUid equals p.ProviderUid
join r in this.context.Regions on p.RegionUid equals r.RegionUid
join o in this.context.Offices on s.OfficeUid equals o.OfficeUid
join u in this.context.Users on sm.UserUid equals u.UserUid
select new { s, sd, p, r, o, u })
.Where(g => this.GetSearchCriteria(g))
.Select(g => g);