我是 LINQ 的新手,有人可以帮我如何将以下查询转换为 LINQ:
select c.cityname, c.cityid from country cn
inner join [State] st on cn.stateid = st.stateid
inner join [city] c on c.cityid = st.cityid where
cn.countrid = 'cn001' order by c.cityname
我是 LINQ 的新手,有人可以帮我如何将以下查询转换为 LINQ:
select c.cityname, c.cityid from country cn
inner join [State] st on cn.stateid = st.stateid
inner join [city] c on c.cityid = st.cityid where
cn.countrid = 'cn001' order by c.cityname
尝试这个:
List<State> States = new List<State>();
List<City> Cities = new List<City>();
List<Country> Countries = new List<Country>();
int countryId = 10;
var query = Countries
.Join(States, c => c.StateId, s => s.StateId, (c, s) => new {Country = c, State = s})
.Join(Cities, s => s.State.CityId, c => c.CityId, (t, c) => new {c.CityId, c.CityName, t.Country.CountryId})
.Where(r => r.CountryId == countryId)
.Select(r => new {r.CityName, r.CityId})
.OrderBy(r => r.CityName);
class Country
{
public int StateId { get; set; }
public int CountryId { get; set; }
}
class State
{
public int StateId { get; set; }
public int CityId { get; set; }
}
class City
{
public int StateId { get; set; }
public int CityId { get; set; }
public string CityName { get; set; }
}