Entity framework core 包含子查询的Linq在EF Core上失败

Entity framework core 包含子查询的Linq在EF Core上失败,entity-framework-core,Entity Framework Core,我正在使用MySQL DB和ef core,所有这些都可以正常工作,但是下面的查询并没有返回预期的结果 var query = _context.ServiceData. Include(x => x.Country) .Include(x=>x.Country.CountryLocale) .Where(x=>x.Country.CountryLocale.Any(l=>l.Locale == "en-US"));

我正在使用MySQL DB和ef core,所有这些都可以正常工作,但是下面的查询并没有返回预期的结果

    var query = _context.ServiceData.
        Include(x => x.Country)
        .Include(x=>x.Country.CountryLocale)
        .Where(x=>x.Country.CountryLocale.Any(l=>l.Locale == "en-US"));
在执行和执行之后

query.First().Country.CountryLocale.Count // Returns count of greater than 1 when expected count is 1
该表只有2个条目,当预期只有1个条目时,上述两个条目都会被提取

表格布局

Id   Name Locale
1    Test   en-US
2    Test   en-GB
我所看到的所有例子似乎都表明我是如何做的,所以我不确定我遗漏了什么

下面是上面的Linq生成的sql

SELECT `a`.`Id`, `a`.`ServiceDataCode`, `a`.`CountryId`, `a`.`Enabled`, `a`.`LastUpdated`, `a`.`TimezoneId`, `c`.`Id`, `c`.`DialingCode`, `c`.`Enabled`, `c`.`IsoNumeric`, `c`.`IsoThreeLetterCode`, `c`.`IsoTwoLetterCode`, `c`.`LastUpdated`, `c0`.`Id`, `c0`.`IsoTwoLetterCode`, `c0`.`LastUpdated`, `c0`.`Locale`, `c0`.`Name`
FROM `ServiceData` AS `a`
INNER JOIN `CountryData` AS `c` ON `a`.`CountryId` = `c`.`Id`
LEFT JOIN `CountryLocale` AS `c0` ON `c`.`IsoTwoLetterCode` = `c0`.`IsoTwoLetterCode`
WHERE EXISTS (
    SELECT 1
    FROM `CountryLocale` AS `c1`
    WHERE (`c`.`IsoTwoLetterCode` = `c1`.`IsoTwoLetterCode`) AND (`c1`.`Locale` = 'en-US'))
ORDER BY `a`.`Id`, `c`.`Id`, `c0`.`Id`

我想存储过程是另一种选择,但我不想这样做。

好吧,下面是一个对象查询,其中包含属于first service数据的国家/地区和特定国家/地区的正确路径:

using (var query = _context.ServiceData
    .Include("Country.CountryLocale")
    .Where(x => x.ServiceData.CountryId == x.Country.Id)
    .Where(x => x.Country.IsoTwoLetterCode == x.Country.CountryLocale.IsoTwoLetterCode)
    .Where(x => x.Country.CountryLocale.Locale == "en-US"))
{
    query.FirstOrDefault();
}

希望这会有所帮助。

谢谢,但仍然会将en US和en GB从CountryLocale表格中删除。欢迎您。我已更改为更合适的筛选器,因此您可以查看更改。感谢您再次尝试,但无法执行x.Country.CountyLocale==x.Country.CountyLocale.IstoWoletterCode as Country.CountryLocale是一个ICollection。我理解。过滤器的一部分应该是这样的。其中(x=>x.Country.CountryLocale.Any=>l.Locale==“en-US”)再次感谢,但仍然是相同的结果现在生成的sql与我原来的问题相同,但左连接左连接
CountryLocale
as
c0
c
IsoTwoLetterCode
=
c0
和内部连接
CountryData
a
c
yId=
c
Id
看不出正确,我相信您会同意的。