Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用中间的方法将c#列表传输到另一个列表_C#_.net_List_Collections - Fatal编程技术网

使用中间的方法将c#列表传输到另一个列表

使用中间的方法将c#列表传输到另一个列表,c#,.net,list,collections,C#,.net,List,Collections,我不确定标题,但我有一个任务,我有麻烦 我有一个从WCF服务下载的列表 即: 基本上,我需要做的是通过以下操作为列表中的每个记录生成地理坐标值: GeoCoordinate(Convert.ToDouble(LatitudeField), Convert.ToDouble(LongitudeField)); 然后将每个记录中的地理坐标值添加到一个新列表中,然后我就可以使用它了 有道理吗? 我不知道该怎么办。 我是否需要某种foreach循环来对原始列表进行地理坐标转换?我必须先将其转换为类才能

我不确定标题,但我有一个任务,我有麻烦

我有一个从WCF服务下载的列表

即:

基本上,我需要做的是通过以下操作为列表中的每个记录生成地理坐标值:

GeoCoordinate(Convert.ToDouble(LatitudeField), Convert.ToDouble(LongitudeField));
然后将每个记录中的地理坐标值添加到一个新列表中,然后我就可以使用它了

有道理吗? 我不知道该怎么办。 我是否需要某种foreach循环来对原始列表进行地理坐标转换?我必须先将其转换为类才能这样做吗

谢谢,任何帮助或想法欢迎


(编辑:只需在windows phone上添加,因此.net 4可用)

如果您使用的是.net 3.5或更高版本,则可以使用LINQ2对象

var coordinateList = StopsList.Select(stop =>
    new GeoCoordinate(Convert.ToDouble(stop.LatitudeField),
                      Convert.ToDouble(stop.LongitudeField))).ToList();
如果您使用的是较旧的.NET版本,则需要使用显式循环

var coordinateList = new List<GeoCoordinate>();
foreach(var stop in StopsList)
{
    coordinateList.Add(
        new GeoCoordinate(Convert.ToDouble(stop.LatitudeField),
                          Convert.ToDouble(stop.LongitudeField)));
}
您也可以对创建的类执行相同的操作,将
new{
替换为
newyourclass{

您还可以使用使用上述两个列表的方法

var combinedList = StopsList.Zip(coordinateList, Tuple.Create).ToList();
这将为您提供一个
列表


请注意,在大多数情况下,您可以跳过
.ToList()
,您不会得到一个
列表,而是一个
IEnumerable
。在大多数情况下,它与
列表
一样有效,但您的程序不必一直将所有内容复制到列表中。通常效率更高一点,您不需要键入
.ToList()
始终。

在引入Linq之前,您可以使用返回到.NET 2.0的ConvertAll方法

以下是一个例子:

和MSDN文章:


谢谢,这非常有用。我想我已经完成了一半,因为我可能需要创建一个新列表,并将旧列表中的值与新列表中的值相结合。因此,基本上我的旧列表中也包含了这个地理坐标值。谢谢。谢谢,我使用了第一个建议和新的YourClass位:-)非常有用!
var coordinateList = new List<GeoCoordinate>();
foreach(var stop in StopsList)
{
    coordinateList.Add(
        new GeoCoordinate(Convert.ToDouble(stop.LatitudeField),
                          Convert.ToDouble(stop.LongitudeField)));
}
var combinedList = StopsList.Select(stop =>
    new {
        s = stop,
        coord = new GeoCoordinate(Convert.ToDouble(stop.LatitudeField),
                      Convert.ToDouble(stop.LongitudeField)),
    }).ToList();
var combinedList = StopsList.Zip(coordinateList, Tuple.Create).ToList();