在 Linq 查询中使用 into 和 from

本文关键字:into from Linq 查询 | 更新日期: 2023-09-27 17:56:15

>我有以下linq查询:

var allLocationRates = (from homeLocation in clientLocations
                        from hostLocation in clientLocations
                        select new PaymentRateTrip
                        {
                            HomeCountryId = homeLocation.CountryId,
                            HomeCountry = homeLocation.CountryName,
                            HostCountryId = hostLocation.CountryId,
                            HostCountry = hostLocation.CountryName,
                            HomeLocationId = homeLocation.LocationId,
                            HomeLocation = homeLocation.LocationName,
                            HostLocationId = hostLocation.LocationId,
                            HostLocation = hostLocation.LocationName
                        }
                        into allLocations
                        from l in allLocations // <-- error on this line
                        // continues on...

我正在尝试获得所有可能的clientLocations组合. 但是,在上面的查询中,我在from l in allLocations行上收到错误。 上面写着:

在后续版本中不允许使用类型为"付款率旅行"的表达式 具有源类型的查询表达式中的 from 子句 'System.Collections.Generic.IEnumerable'. 类型 推理在调用"SelectMany"时失败。

为什么它只选择单个PaymentRateTrip而应该选择它们的列表?

在 Linq 查询中使用 into 和 from

在 select 子句后使用关键字 into 时,您正在执行"查询延续",这意味着所有后续 linq-code 都在 into 之后的名称上运行。你不需要"from l in allLocations"一行,只需在into后使用"l",比如:

}
into l
// continues on...
// For example: select l.HomeCountryId

有关查询延续的更多信息