Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ查询对象树下的值需要匹配_C#_Linq - Fatal编程技术网

C# LINQ查询对象树下的值需要匹配

C# LINQ查询对象树下的值需要匹配,c#,linq,C#,Linq,您好,提前谢谢您的帮助 我有一个PostalCode对象,它有一个与之关联的城市列表,一个城市有一个state对象作为属性,state对象有一个country对象作为属性 我需要在返回城市的邮政编码列表上运行linq查询,但我需要它位于国家代码等于“US”的位置 在SQL中,它是关系上的一组内部联接: SELECT * FROM PostalCodes pc INNER JOIN CityPostalCode cpc ON cpc.PostalCodeId = pc.Id INNER JOIN

您好,提前谢谢您的帮助

我有一个PostalCode对象,它有一个与之关联的城市列表,一个城市有一个state对象作为属性,state对象有一个country对象作为属性

我需要在返回城市的邮政编码列表上运行linq查询,但我需要它位于国家代码等于“US”的位置

在SQL中,它是关系上的一组内部联接:

SELECT *
FROM PostalCodes pc
INNER JOIN CityPostalCode cpc
ON cpc.PostalCodeId = pc.Id
INNER JOIN Cities c 
ON cpc.CityId = c.Id
INNER JOIN States s
ON s.Id = c.StateId
INNER JOIN Country c
ON s.CountryId = c.Id
WHERE pc.Code = '12345' AND c.Code = 'US'
如何在LINQ中的对象上执行此操作

public class PostalCode
{

    public int Id { get; set; }
    public string Code { get; set; }

    public virtual List<City> Cities { get; set; } 
}

public class City
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int StateId { get; set; }
    public virtual State State { get; set; }
    public virtual List<PostalCode> PostalCodes { get; set; }
}
public class State
{
    public int Id {get; set;}
    public string Code {get; set;}
    public string Name {get; set;}
    public int CountryId {get; set;}
    public Country Country {get; set;}
}
public class Country
{

    public int Id { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
    public virtual List<State> States { get; set; }
}
公共类后代码
{
公共int Id{get;set;}
公共字符串代码{get;set;}
公共虚拟列表城市{get;set;}
}
公营城市
{
公共int Id{get;set;}
公共字符串名称{get;set;}
public int StateId{get;set;}
公共虚拟状态状态{get;set;}
公共虚拟列表PostalCodes{get;set;}
}
公共阶级国家
{
公共int Id{get;set;}
公共字符串代码{get;set;}
公共字符串名称{get;set;}
public int CountryId{get;set;}
公共国家{get;set;}
}
公营国家
{
公共int Id{get;set;}
公共字符串代码{get;set;}
公共字符串名称{get;set;}
公共虚拟列表状态{get;set;}
}

以下是您的操作方法:

var result =
    listOfPostalCodes
    .Where(pc => pc.Code == "12345") //Filter postal codes
    .SelectMany(pc => pc.Cities) //Select all cities
    .Where(c => c.Country.Code == "US") //Of which country code is equal to US
    .ToList();

如果要返回包含该邮政编码的城市列表,则可以执行以下操作:

var cities = context.Cities
    .Where(city => city.State.Country.Code == "US" &&
                   city.PostalCodes.Any(pc => pc.Code == "12345"))
    .ToList();
见下面的代码:

postalCodes.Where(p => p.Code == "12345" &&
   p.Cities.Where(c => c.State.Country.Code == "US").Any());

这将返回邮政编码。在我看来,OP想要返回城市。问题规定:从PostalCodes pc中选择*