Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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# 将此代码优化为Parallel.ForEach或C中任何更好的方式_C#_Multithreading_Foreach - Fatal编程技术网

C# 将此代码优化为Parallel.ForEach或C中任何更好的方式

C# 将此代码优化为Parallel.ForEach或C中任何更好的方式,c#,multithreading,foreach,C#,Multithreading,Foreach,我有一段代码。如何将其转换为Parallel.ForEach?我尝试使用线程来优化代码。有人能帮我得到输出吗 List<BOUserShoutoutResponseData> result = conList.Select(con => { List<string> totalShoutoutImages = new List<string>(); totalSh

我有一段代码。如何将其转换为Parallel.ForEach?我尝试使用线程来优化代码。有人能帮我得到输出吗

  List<BOUserShoutoutResponseData> result = conList.Select(con =>
            {
                List<string> totalShoutoutImages = new List<string>();
                totalShoutoutImages = ShoutoutMultipleImages(con.ShoutoutId, con.UserId, con.ShoutoutImageName, con.IsImageSync, shoutoutImages);
                return new BOUserShoutoutResponseData()
                {
                    UserName = (con.FirstName ?? string.Empty) + " " + (con.LastName ?? string.Empty),
                    UserId = (Guid)con.UserId,
                    Distacne =
                        Convert.ToDecimal(
                            Math.Round(
                                ConvertDistance.DistanceTo(Convert.ToDouble(request.UserLatitude),
                                    Convert.ToDouble(request.UserLongitude), Convert.ToDouble(con.UserLatitude),
                                    Convert.ToDouble(con.UserLongitude), request.DistanceType[0]), 12)),
                    DistacneTemp = Convert.ToDecimal(con.Distance),
                    ShoutoutLatitude = Convert.ToString(con.ShoutoutLatitude),
                    ShoutoutLongitude = Convert.ToString(con.ShoutoutLongitude),
                    ShoutoutTypeId = con.ShoutoutTypeId ?? 0,
                    ShoutoutType = con.ShoutoutType ?? string.Empty,
                    PostTypeId = con.PostTypeId ?? 0,
                    PostType = con.PostTypeName ?? string.Empty,
                    Description = con.Description ?? string.Empty,
                    TotalLike = (int)con.TotalLike,
                    Url = con.Url ?? string.Empty,
                    PlaceId = con.PlaceId ?? string.Empty,
                    AddressComponents = 
                    GetAddressComponentOfShoutout((long)con.ShoutoutId),
                    UserSmallImagePath =
                        string.IsNullOrEmpty(con.ImageName)
                            ? string.Empty
                            : GetUserImagePath(con.ImageName + "--S", 
    (bool)con.IsProfileImageSync),
GetShoutoutImagePath(con.UserId.ToString(), con.ShoutoutImageName + "--M", 
(bool)con.IsImageSync),
                    ShoutoutImages = totalShoutoutImages ?? null                        
        return result;
为什么不添加AsParallel并将Linq转换为并行Linq


然后,您可能希望使用WithDegreeOfParallelism…、WithMergeOptions…、优化查询。。。等等。选项

我会给你一个线索,Select只是在引擎盖下做一个foreach。除此之外,这显示了一个问题,我还建议您在并行运行时要谨慎,以使codez更快速。这可能是真的,也可能不是真的,这取决于您的实现和许多其他变量,哪些是conList和SHOUTOUTOMultipleImages,哪些需要花费很长时间才能使用并行性?TotalShoutoTimages从未在本代码中使用TotalShoutoTimages未被使用,因此请删除,为什么?这可能有用,也可能没有帮助。TotalShoutoTimages从不使用。将并行性用于简单的投影听起来很奇怪。也许应该多张图片有副作用?
List<BOUserShoutoutResponseData> result = conList
  .AsParallel() // Same Linq but doing in parallel
//.AsOrdered()  // uncomment if you want to preserve items' order
  .Select(//TODO: check totalShoutoutImages usage
          con => new BOUserShoutoutResponseData() { 
            UserName     = string.Join(" ", con.FirstName, con.LastName),
            PostTypeId   = con.PostTypeId ?? 0,
            PostType     = con.PostTypeName ?? string.Empty,
            Description  = con.Description ?? string.Empty,
            TotalLike    = (int)con.TotalLike,
            TotalComment = (int)con.TotalComment
            ... 
          })
  .ToList();

return result;