LINQ在嵌套列表中选择表达式

时间:2015-10-01 15:09:31

标签: c# .net linq

我如何使用Linq循环遍历嵌套列表?

我有一个嵌套列表,如下所示:

var bookings = new List<List<Booking>>();

预订舱内有预订详情,等等

我想循环遍历每个单独的列表并提取使用Linq嵌套的列表,但我很难做到:(

我不确定我应该使用什么方法与Linq,我尝试过使用SelectMany,但我认为我不太了解它。

1 个答案:

答案 0 :(得分:2)

如果您尝试使用SelectMany展平列表:

bookings.SelectMany((list) => list);

如果您尝试提取每个列表:

bookings.ForEach((list) => {
    // Do something with the nested list
});

如果您尝试过滤嵌套列表,但保留列表:

bookings.Select((list) => list.Where((item) => item.Price > 20));

等等。

SelectMany需要一个功能,可以将列表中的每个项目转换为IEnumerable<T>,然后将生成的IEnumerable<T>IEnumerable<T>展平为IEnumerable<T>。通过将SelectMany与身份函数(list) => list一起使用,您对列表中的每个项目实际上无效 - 它们已经IEnumerable<T>'s,因此,不需要 任何进一步的转换 - SelectMany随后将展平IEnumerable<T> IEnumerable<T>var markers = [ [1002, -14.2350040, -51.9252800], [2000, -34.028249, 151.157507], [123, 39.0119020, -98.4842460], [50, 48.8566140, 2.3522220], [22, 38.7755940, -9.1353670], [12, 12.0733335, 52.8234367], ]; function initializeMaps() { var myLatLng = { lat: -25.363, lng: 131.044 }; var map = new google.maps.Map(document.getElementById('map_canvas'), { zoom: 4, center: myLatLng }); var bounds = new google.maps.LatLngBounds(); markers.forEach(function(point) { generateIcon(point[0], function(src) { var pos = new google.maps.LatLng(point[1], point[2]); bounds.extend(pos); new google.maps.Marker({ position: pos, map: map, icon: src }); }); }); map.fitBounds(bounds); } var generateIconCache = {}; function generateIcon(number, callback) { if (generateIconCache[number] !== undefined) { callback(generateIconCache[number]); } var fontSize = 16, imageWidth = imageHeight = 35; if (number >= 1000) { fontSize = 10; imageWidth = imageHeight = 55; } else if (number < 1000 && number > 100) { fontSize = 14; imageWidth = imageHeight = 45; } var svg = d3.select(document.createElement('div')).append('svg') .attr('viewBox', '0 0 54.4 54.4') .append('g') var circles = svg.append('circle') .attr('cx', '27.2') .attr('cy', '27.2') .attr('r', '21.2') .style('fill', '#2063C6'); var path = svg.append('path') .attr('d', 'M27.2,0C12.2,0,0,12.2,0,27.2s12.2,27.2,27.2,27.2s27.2-12.2,27.2-27.2S42.2,0,27.2,0z M6,27.2 C6,15.5,15.5,6,27.2,6s21.2,9.5,21.2,21.2c0,11.7-9.5,21.2-21.2,21.2S6,38.9,6,27.2z') .attr('fill', '#FFFFFF'); var text = svg.append('text') .attr('dx', 27) .attr('dy', 32) .attr('text-anchor', 'middle') .attr('style', 'font-size:' + fontSize + 'px; fill: #FFFFFF; font-family: Arial, Verdana; font-weight: bold') .text(number); var svgNode = svg.node().parentNode.cloneNode(true), image = new Image(); d3.select(svgNode).select('clippath').remove(); var xmlSource = (new XMLSerializer()).serializeToString(svgNode); image.onload = (function(imageWidth, imageHeight) { var canvas = document.createElement('canvas'), context = canvas.getContext('2d'), dataURL; d3.select(canvas) .attr('width', imageWidth) .attr('height', imageHeight); context.drawImage(image, 0, 0, imageWidth, imageHeight); dataURL = canvas.toDataURL(); generateIconCache[number] = dataURL; callback(dataURL); }).bind(this, imageWidth, imageHeight); image.src = 'data:image/svg+xml;base64,' + btoa(encodeURIComponent(xmlSource).replace(/%([0-9A-F]{2})/g, function(match, p1) { return String.fromCharCode('0x' + p1); })); } initializeMaps();