我正在使用下面的课程
class Country
{
int CtryID {get; set;}
List<City> city {get; set;}
}
class City
{
string county {get; set;}
int sqkm {get; set;}
}CtryID的值为1,2,3,县的值为“县1”,“县2”,等等。
我想要这样的结果
1
County 1
County 6
County 3
2
County 9
County 4
County 2如何使用Lambda查询实现这一点?
这是我用过的。这个列表叫做ListA
var lst = from aa in ListA
from cny in aa.ctryid
select new
{
CountryID= aa.CtryID,
CountyName= cny.County
};查询2:在我的第二个查询(从第一个查询中分离出来)中,我想按国家排序,然后按全国的每个城市排序,而不对每个城市重复国家一次?
发布于 2010-05-20 13:25:29
var countryGroups = (from aa in ListA
from cny in aa.ctryid
select new
{
CountryID= aa.CtryID,
CountyName= cny.County
}).GroupBy(n => n.CountryID);这将按CountryID对结果进行分组,但是输出不会是一个列表--它将是分组。
要输出,在countryGroups上迭代,在其中的每一个中迭代CountyName对象。
https://stackoverflow.com/questions/2871940
复制相似问题