1

我有两个类,其中一个是 Destinations,另一个是 DestinationDetails

public class Destinations
{
    public Destinations() { }

    public string CarrierName { get; set; }

    public List<DestinationDetails> Details { get; set; }
}

public class DestinationDetails
{
    public DestinationDetails() { }

    public string Destination { get; set; }

    public string Type { get; set; }

}

我想string "Destination"从第一类的对象列表中获取第二类的所有内容

我有List<Destinations>而且我不想使用for loop or foreachstatments

4

3 回答 3

1
var dest = new Destinations();

//Initialize the details

var destNames = dest.Details.Select(d => d.Destination).ToList();
于 2013-04-14T03:51:05.730 回答
1

你在寻找这样的东西吗?

     var det = new Destinations();
     det.Details = new List<DestinationDetails>();
      det.Details.Add(new DestinationDetails() { Destination = "CA" });
      det.Details.Add(new DestinationDetails() { Destination = "NJ" });
      ...
      ...
     var details = new DestinationDetails();
     details.Destination = string.Join(",",det.Details.Select(x => x.Destination).ToArray() );

更新:-

提供了目的地列表“allDet”,您可以获得如下字符串列表:-

  alldet.Where(x => x.Details != null).SelectMany(x => x.Details.Select(y => y.Destination)).ToList() //With out ToList() it will give you IEnumerable<String>
于 2013-04-14T03:53:33.423 回答
0

List<Destinations> AirportDestinations ;// 这个列表中的Destinations对象DetailsDestination

所以通过使用SelectMany

List<string> cities.AddRange(AirportDestinations.Where(x => x.Details != null).SelectMany(d => d.Details.Select(s => s.Destination)));

现在您拥有Destination列表中的所有对象

于 2013-04-14T04:23:09.533 回答