C# 如何使用linq resultset c添加或预挂起硬编码数据#
假设下面的查询返回城市ID和城市名称,但如果我想C# 如何使用linq resultset c添加或预挂起硬编码数据#,c#,linq,C#,Linq,假设下面的查询返回城市ID和城市名称,但如果我想pre pend一些带有linq结果的硬编码数据,那么我该怎么做呢 var cityList = network.Continents .SelectMany(continent => continent.Countries) .Where(ctry => ctry.Id == "country") .Se
pre pend
一些带有linq
结果的硬编码数据,那么我该怎么做呢
var cityList = network.Continents
.SelectMany(continent => continent.Countries)
.Where(ctry => ctry.Id == "country")
.SelectMany(ctry => ctry.Cities,
c => new City { Id = c.Id, Name = c.Name })
.ToList();
上面的查询将以这种方式返回数据
City ID City Name
-------- ------------
1 Bombay
2 Kolkata
3 Delhi
但我希望这样的结果
City ID City Name
--------- -----------
0 --Select City--
1 Bombay
2 Kolkata
3 Delhi
因此,我想将这个数据0添加为ID,并将--Select City--as Name
添加到返回linq查询的列表中。帮助我获取示例代码。谢谢嗯。。。只需插入()it
var cityList = network.Continents
.SelectMany(continent => continent.Countries)
.Where(ctry => ctry.Id == "country")
.SelectMany(ctry => ctry.Cities,
c => new City { Id = c.Id, Name = c.Name })
.ToList();
// yup, this is what I would do...
cityList.Insert(0, new City{ Id = 0, Name = "--Select City--"});
嗯。。。只需插入()it
var cityList = network.Continents
.SelectMany(continent => continent.Countries)
.Where(ctry => ctry.Id == "country")
.SelectMany(ctry => ctry.Cities,
c => new City { Id = c.Id, Name = c.Name })
.ToList();
// yup, this is what I would do...
cityList.Insert(0, new City{ Id = 0, Name = "--Select City--"});
具有
可枚举的解决方案。Concat
City[] prepend = new City[] {new City { Id = 0, Name = "--Select City--"}};
IEnumerable<City> cities = network.Continents
.SelectMany(continent => continent.Countries)
.Where(ctry => ctry.Id == "country")
.SelectMany(ctry => ctry.Cities,
c => new City { Id = c.Id, Name = c.Name });
var citySequence = prepend.Concat(cities);
var cityList = citySequence.ToList();
City[]prepend=new City[]{new City{Id=0,Name=“--Select City--”};
IEnumerable cities=网络。大陆
.SelectMany(大陆=>大陆国家)
.Where(ctry=>ctry.Id==“国家”)
.SelectMany(ctry=>ctry.Cities,
新城市{Id=c.Id,Name=c.Name});
var citySequence=prepend.Concat(城市);
var cityList=citySequence.ToList();
可枚举的解决方案。Concat
City[] prepend = new City[] {new City { Id = 0, Name = "--Select City--"}};
IEnumerable<City> cities = network.Continents
.SelectMany(continent => continent.Countries)
.Where(ctry => ctry.Id == "country")
.SelectMany(ctry => ctry.Cities,
c => new City { Id = c.Id, Name = c.Name });
var citySequence = prepend.Concat(cities);
var cityList = citySequence.ToList();
City[]prepend=new City[]{new City{Id=0,Name=“--Select City--”};
IEnumerable cities=网络。大陆
.SelectMany(大陆=>大陆国家)
.Where(ctry=>ctry.Id==“国家”)
.SelectMany(ctry=>ctry.Cities,
新城市{Id=c.Id,Name=c.Name});
var citySequence=prepend.Concat(城市);
var cityList=citySequence.ToList();
见这一行City[]prepend=new City[]
意味着只需添加一个小条目,我就需要创建一个额外的类……它有意义吗?@Thomas,它不是一个额外的类,它是一个额外的对象。因为您使用的是列表,所以插入新项更简单,因为列表支持插入。但如果您需要结果作为数组或只是IEnumerable,Concat将非常有用。请参见这一行City[]prepend=new City[]
的意思是只需添加一个小条目,我就需要创建一个额外的类………它有意义吗?@Thomas,它不是额外的类,而是额外的对象。因为您使用的是列表,所以插入新项更简单,因为列表支持插入。但若您需要结果作为数组或IEnumerable,Concat将非常有用