简体   繁体   中英

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.

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. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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