简体   繁体   English

查询特定班级成员的字典C#

[英]Query dictionary for specific class members c#

I have the following class: 我有以下课程:

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;
        }
    }
}

In my main function, I'm declaring the following: 在主要功能中,我声明了以下内容:

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");
    }
}

I'm trying to get a dictionary ( Dictionary<uint, doubule> ) for, let's say, only the minimum values. 我正在尝试获取仅用于最小值的字典( Dictionary<uint, doubule> )。

Meaning, that my dictionary will have the following: 意思是,我的字典将具有以下内容:

101, 1
102, 4
103, 7

I tried many variations of LINQ , but just couldn't get it right. 我尝试了LINQ许多变体,但无法正确完成。 Is it possible? 可能吗?

使用.ToDictionary() (MSDN文档)

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

做这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM