Entity framework EF核心。任何未过滤的结果
我有以下sql,我正在尝试将其转换为linq:Entity framework EF核心。任何未过滤的结果,entity-framework,linq,entity-framework-core,Entity Framework,Linq,Entity Framework Core,我有以下sql,我正在尝试将其转换为linq: SELECT * FROM [Service] s inner join vendor v on vendorid=v.id inner join VendorLocation vl on vl.VendorId=v.id where s.active=1 and v.active=1 and vl.City = 'toronto' and vl.Active=1 我有一个属于某个供应商的服务,该供应商有位置。我试图根据城市筛选
SELECT *
FROM [Service] s
inner join vendor v on vendorid=v.id
inner join VendorLocation vl on vl.VendorId=v.id
where s.active=1 and v.active=1 and vl.City = 'toronto' and vl.Active=1
我有一个属于某个供应商的服务,该供应商有位置。我试图根据城市筛选位置,但查询返回的结果不满足“.Any”子句中的条件
sql语句返回正确的结果,但linq不正确。感谢您的帮助,谢谢!Ben您好,您现在正在使用EF Core,但是 我建议使用LINQ,因为它最简单 请阅读此了解更多信息 这是示例代码
var innerGroupJoinQuery2 =
from category in categories
join prod in products on category.ID equals prod.CategoryID into prodGroup
from prod2 in prodGroup
where prod2.UnitPrice > 2.50M
select prod2;
您可以尝试使用查询表示法:
var query = from v in _context.Vendors
join s in v.Services on v.Id equals s.VendorId
join l in v.ServiceLocations on v.Id equals l.VendorId
where v.Active && s.Active && l.City=="Toronto"
select new {v,s,l};
var result= await query.ToLinqAsync();
查询返回的结果不满足“.Any”子句中的条件
这些查询只是不一样。SQL查询为每个VendorLocation返回一行,其中包含来自联接表的附加列,并投影所有列
SELECT *
FROM [Service] s
inner join vendor v on vendorid=v.id
inner join VendorLocation vl on vl.VendorId=v.id
where s.active=1 and v.active=1 and vl.City = 'toronto' and vl.Active=1
在LINQ中,这将类似于
from vl in VendorLocation
where vl.Vendor.Active
&& vl.Vendor.Service.Active
&& vl.Active
&& vl.City = 'toronto'
select new
{
ServiceName = vl.Vendor.Service.Name,
ServiceDescription = vl.Vendor.Service.Description,
. . .
VendorName = vl.Vendor.Name,
VendorWhatever = vl.Vendor.Whatever,
. . .
vl.Name,
. . .
};
Any
不过滤位置,而是过滤服务。不支持筛选的Include
s。您尝试实现的结果是什么,以及由三个实体组成的对象符合条件?谢谢,这解释了为什么不筛选位置感谢响应。你的回答引导我走上正确的道路。在您的示例中,S包含ServiceLocations,但筛选仍不起作用,但L已正确筛选。所以我只是用L和S来代替v。解决方案不是最优的,但至少是可行的。
from vl in VendorLocation
where vl.Vendor.Active
&& vl.Vendor.Service.Active
&& vl.Active
&& vl.City = 'toronto'
select new
{
ServiceName = vl.Vendor.Service.Name,
ServiceDescription = vl.Vendor.Service.Description,
. . .
VendorName = vl.Vendor.Name,
VendorWhatever = vl.Vendor.Whatever,
. . .
vl.Name,
. . .
};