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

时间:2019-09-20 10:45:29

标签: c# multithreading foreach

我有一段代码。如何将其转换为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;

1 个答案:

答案 0 :(得分:2)

为什么不添加AsParallel()并将 Linq 变成 Parallel Linq

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;

然后,您可能希望使用WithDegreeOfParallelism(...)WithMergeOptions(...)等选项来调整查询。