Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.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# 使用LINQ在C中将两个列表放在一起_C#_Windows_Linq_Google Maps_Foreach - Fatal编程技术网

C# 使用LINQ在C中将两个列表放在一起

C# 使用LINQ在C中将两个列表放在一起,c#,windows,linq,google-maps,foreach,C#,Windows,Linq,Google Maps,Foreach,我对LINQ和编程有点陌生。我想做的是把两个不同的列表放在一起,所以我不想使用两个foreache循环,而是想用Linq获取信息。 我将向您展示我的代码示例: Country c = CountriesHandler.GetCountry(startPage.SelectedCountry); if (globalSite) { List<Marker> markersForGlobal = new

我对LINQ和编程有点陌生。我想做的是把两个不同的列表放在一起,所以我不想使用两个foreache循环,而是想用Linq获取信息。 我将向您展示我的代码示例:

  Country c = CountriesHandler.GetCountry(startPage.SelectedCountry);
            if (globalSite)
            {
                List<Marker> markersForGlobal = new List<Marker>();
                foreach (var user in userList)
                {
                    Country ce = CountriesHandler.GetCountry(user.GetAttributeValue<string>("Country"));

                    foreach (var u in photoWithInfo)
                    {
                        if (user.ID == u.UserID)
                        {
                            int id = u.UserID;
                            string im = u.SquareThumbnailUrl;

                            markersForGlobal.Add(new Marker
                                   {
                                       Id = id,
                                       Image = im,
                                       Longitude = ce.Longitude,
                                       Latitude = ce.Latitude
                                   });
                            break;
                        }
                    }
                }

                return Json(markersForGlobal);
            }
这就是它看起来的样子,现在要在谷歌地图上列出它需要大量的内存,所以我认为你可以用更好的解决方案来做到这一点。
感谢您的时间

我不确定在您的示例中如何实现这一点,因为您使用第一个列表来获取需要进入其他列表的数据。我不知道你的GetCoutry方法做了什么,但如果它经常在DB上运行,或者如果它是你正在调用的另一个服务,那可能是你的瓶颈。不要为每个用户重复执行一个操作,而是尝试通过一次呼叫获取列表中所有用户的所有国家/地区。

可以尝试的一种方法是使用类似于下面给出的LINQ查询。一个缺点是GetCountry被调用了两次

var result = from pwi in photoWithInfo
                     join user in userList on pwi.UserId equals user.UserId
                     select new Marker()
                     {
                         Id = user.UserId,
                         Image = pwi.SquareThumbnailUrl,
                         Longitude = CountriesHandler.GetCountry(user.GetAttributeValue<string>("Country")).Longitude,
                         Latitude = CountriesHandler.GetCountry(user.GetAttributeValue<string>("Country")).Latitude
                     };

试试这个linq,我不认为它会减少内存,但肯定它更优雅:

var markers = new List<Marker>();

userList.ForEach(user =>
{
    var country = CountriesHandler
                    .GetCountry(user.GetAttributeValue<string> ("Country"));

    markers.AddRange(photoWithInfo.Where(info => user.Id == info.UserID)
        .Select(info => new Marker
            {
                Id = info.UserID,
                Image = info.SquareThumbnailUrl,
                Latitude = country.Latitude,
                Longitude = country.Longitude
             }));
    });

什么是photoWithInfo?photoWithInfo是对数据库调用的属性,如用户ID、图像感谢您的时间!谢谢你抽出时间,罗布!我在考虑使用metod,在数据库中加入一个连接,从表中获取所有国家,然后与用户或其他人连接。。呵呵,你应该使用let来避免执行GetCountry两次,你还应该在最后的结果上应用ToList来满足OP的要求。非常感谢!你是我最好的朋友吗?哈哈哈!:你对let是什么意思?从未使用过b4:@KingKing@user2862542let是允许我们在表达式linq查询中定义一些变量的关键词,请尝试阅读此内容,非常感谢@KingKing,但我不需要在此处使用let,仍在工作呵呵: