查询特定类成员的字典c#

时间:2013-12-18 13:45:54

标签: c# linq dictionary

我有以下课程:

using System;
using System.Collections.Generic;

namespace ddd
{
    public class FPSData
    {
        private double minFPS;
        private double maxFPS;
        private double averageFPS;

        #region Properties

        public double MinFPS
        {
            get { return minFPS; }
            set { minFPS = value; }
        }
        public double MaxFPS
        {
            get { return maxFPS; }
            set { maxFPS = value; }
        }
        public double AverageFPS
        {
            get { return averageFPS; }
            set { averageFPS = value; }
        }

        #endregion Properties

        public FPSData(double min, double max, double avg)
        {
            minFPS = min;
            maxFPS = max;
            averageFPS = avg;
        }
    }
}

在我的主要功能中,我宣布以下内容:

class Program
{
    static void Main(string[] args)
    {
        Dictionary<uint, FPSData> trying = new Dictionary<uint, FPSData>();

        FPSData m1 = new FPSData(1, 2, 3);
        FPSData m2 = new FPSData(4, 5, 6);
        FPSData m3 = new FPSData(7, 8, 9);

        trying.Add(101, m1);
        trying.Add(102, m2);
        trying.Add(103, m3);

        Console.WriteLine("sdfgh");
    }
}

我正在尝试获取字典(Dictionary<uint, doubule>),仅举几个最小值。

意思是,我的词典将包含以下内容:

101, 1
102, 4
103, 7

我尝试了LINQ的许多变体,但却无法做到正确。有可能吗?

3 个答案:

答案 0 :(得分:4)

使用.ToDictionary() (MSDN Documentation)

var minimumValues = trying.ToDictionary(k => k.Key, v => v.Value.MinFPS);

答案 1 :(得分:2)

Dictionary<uint, double> result = trying.ToDictionary(x=>x.Key,x=>x.Value.MinFPS);

答案 2 :(得分:1)

这样做:

Dictionary<uint, double> minimumFPS = trying.ToDictionary(k => k.Key, v => v.Value.MinFPS);