序列不包含linq中的元素?

Sequence contains no elements in linq?

我试图在下面的字典的键中获取最低和最高值

public static Dictionary<SimulationResult, List<Student>> SimulationOutput { get; set; } = new Dictionary<SimulationResult, List<Student>>();

这是密钥中的class

internal class SimulationResult
{
    public int Unsolved { get; set; }
    public int ValuePreferences { get; set; }
    public int Index { get; set; }
}

我在下面的行中收到错误消息 Sequence contains no elements

int bestResult = SimulationOutput.Keys.Where(x=> x != null).Select(x => x.ValuePreferences).Max();

也在这些线上

int bestResult = SimulationOutput.Keys.Select(x => x.ValuePreferences).Max();
int LowestUnsonved = SimulationOutput.Keys.Select(x => x.Unsolved).Min();

词典是在并行 for 循环中填充的。

Parallel.For(1, verwerken.Gegevens.AantalIteraties + 1, i =>
{
    PracticumCollectie practicumCollectie = verwerken.Data.PracticumCollectie.Copy();
    StudentCollectie studentCollectie = verwerken.Data.StudentCollectie.Copy();
    studentCollectie.OrderStudentByPreference();
    studentCollectie.AssiningStudentToPracticum(practicumCollectie);

    practicumCollectie.Oplossing.Index = i;
    practicumCollectie.ZetAantalOnopgeloste(studentCollectie);
    practicumCollectie.ZetWaardeVoorkeuren(studentCollectie);

    bool schrijfOplossing = verwerken.Keuze.IsSimuleren ? verwerken.VoldoetAanOndergrenzen(practicumCollectie.Oplossing) : true;

    if (schrijfOplossing)
    {
        SimulationOutput.Add(verwerken.GetBestandsnaam(practicumCollectie.Oplossing), new List<Student>(studentCollectie.Items));
    }
});

如果您正在使用 Parallel,则必须使用 ConcurrentDictionary。

普通词典不是线程安全的。