3

List.AsParallel().ForAll() PLINQ 実装を使用しています。ループ内で条件が成功した場合は、ループが ForAll() ループからすぐに抜け出すようにします。どうすれば達成できますか?

これがサンプルコードです。

using System;
using System.Collections.Generic;
using System.Linq;

namespace Basics
{
class Program
{
    class Family
    {
        public int SNo { get; set; }
        public string Name { get; set; }
    }

    static List<Family> families = null;
    static Program()
    {
        families = new List<Family>()
        {
            new Family(){SNo = 10, Name="Richard"},
            new Family(){SNo = 33, Name="Xio Fung"},
            new Family(){SNo = 10, Name="Sean"},
            new Family(){SNo = 10, Name="Andrea"},
            new Family(){SNo = 20, Name="Bjorn"},
            new Family(){SNo = 20, Name="Isabella"},
            new Family(){SNo = 35, Name="Michael"},
            new Family(){SNo = 35, Name="Marie"}
        };
    }

    private static void Main()
    {
        Dictionary<int, List<Family>> theFamily = new Dictionary<int, List<Family>>();
        var groupedFamilies = families.GroupBy(family => family.SNo);

        groupedFamilies.AsParallel().ForAll(groupedFamily =>
        {
            int groupedFamilyCount = groupedFamily.Count();
            if (groupedFamilyCount == 1)
            {
                Console.WriteLine(groupedFamily.FirstOrDefault().Name);     
                // break; <-- I want to break if I find the count = 1
            }
        });                                 
        Console.ReadLine();
    }
}    

}

よろしく、 スリラム

4

1 に答える 1