我必须通过 LINQ 完成的代码

发布于 2024-10-25 07:50:08 字数 387 浏览 1 评论 0原文

实现解决方案后出错

我有一段代码,我必须通过 LINQ 来完成它

var airlineNameList = new List<string>();

foreach (DTTrip trip in contract_.Trips)
{
    foreach (DTFlight flight in trip.ListOfFlights)
    {
        airlineNameList.Add(flight.AirLineName);
    }
}

如何通过 LINQ 来完成它。 注意:contract_是类的对象。

请帮忙..

Error after implementing the solution

I have a code that i have to do it through LINQ

var airlineNameList = new List<string>();

foreach (DTTrip trip in contract_.Trips)
{
    foreach (DTFlight flight in trip.ListOfFlights)
    {
        airlineNameList.Add(flight.AirLineName);
    }
}

How can do it through LINQ.
Note: contract_ is the object of class.

Please Help..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼眸里的快感 2024-11-01 07:50:09
var airlineNameList = contract_.Trips
                               .SelectMany(t => t.ListOfFlights)
                               .Select(f => f.AirLineName);
var airlineNameList = contract_.Trips
                               .SelectMany(t => t.ListOfFlights)
                               .Select(f => f.AirLineName);
大姐,你呐 2024-11-01 07:50:09
var resultList = contract_.Trips
      .Cast<DTTrip>()
      .SelectMany(trip => trip.ListOfFlights.Cast<DTFlight>())
      .Select(flight => flight.AirLineName)
      .ToList();

难道你不需要Distinct吗?

var resultList = contract_.Trips
      .Cast<DTTrip>()
      .SelectMany(trip => trip.ListOfFlights.Cast<DTFlight>())
      .Select(flight => flight.AirLineName)
      .ToList();

Don't you also need Distinct?

执笏见 2024-11-01 07:50:09
var airlineNameList = (
    from trip in contract_.Trips
    from flight in trip.ListOfFlights
    select flight.AirLineName)
    .ToList();

C# foreach 会为你进行隐式转换(这很恶心),所以也许你需要这个:

var airlineNameList = (
    from trip in contract_.Trips.Cast<DTTrip>()
    from flight in trip.ListOfFlights.Cast<DTFlight>()
    select flight.AirLineName)
    .ToList();
var airlineNameList = (
    from trip in contract_.Trips
    from flight in trip.ListOfFlights
    select flight.AirLineName)
    .ToList();

The C# foreach does implicit casting for you (which is yuck), so perhaps you need this:

var airlineNameList = (
    from trip in contract_.Trips.Cast<DTTrip>()
    from flight in trip.ListOfFlights.Cast<DTFlight>()
    select flight.AirLineName)
    .ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文