简体   繁体   English

字典&lt;(字符串,字符串,字符串),列表<object> &gt; 如何获取与此类字典的 1 个键匹配的值

[英]Dictionary<(string, string, string), List<object>> how to get value that match 1 key of this kind of dictionary

I have a dictionary that uses a combination of string.我有一本使用字符串组合的字典。

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

public class Program
{
    public static void Main()
    {
        List<People> listName = new List<People>(){
        new People(){firstname = "David", middlename = "Broom", lastname = "Lightning", value = 1},
        new People(){firstname = "Dave", middlename = "Cak", lastname = "Londo", value = 2},
        new People(){firstname = "Chris", middlename = "Vanglein", lastname = "Walls", value = 3},
        new People(){firstname = "Rudy", middlename = "Affair", lastname = "Master", value = 4}};

        Dictionary<(string, string, string), List<People>> data = listName
            .Where(x => x.value != 0)
            .GroupBy(x => (x.firstname, x.middlename, x.lastname))
            .ToDictionary(x => x.Key, x => x.ToList());
    }

    public class People {
        public string firstname {get; set;}
        public string middlename {get; set;}
        public string lastname {get; set;}
        public int value {get; set;}
    }
}

https://dotnetfiddle.net/dGTHv7 https://dotnetfiddle.net/dGTHv7

how to get a value when I just have 2 key firstname and lastname.当我只有 2 个关键的名字和姓氏时如何获取值。

I want to get all that match firstname and lastname and ignore middlename.我想获得所有匹配的名字和姓氏并忽略中间名。

There is no point of having a dictionary where the key is a combination of these 3 information if you need to retrieve data with only 2 information.如果您只需要检索仅包含 2 个信息的数据,那么使用字典的键是这 3 个信息的组合是没有意义的。

What about parsing the list directly?直接解析列表怎么样?

var matchingPeople = listName.Where(p => p.firstname == "myfirstname" && p.lastname == "mylastname");

EDITED: Then you need to do that:编辑:然后你需要这样做:

var listName = new List<People>
{
    new People { firstname = "David", middlename = "Broom", lastname = "Lightning", value = 1 },
    new People { firstname = "Dave", middlename = "Cak", lastname = "Londo", value = 2 },
    new People { firstname = "Chris", middlename = "Vanglein", lastname = "Walls", value = 3 },
    new People { firstname = "Rudy", middlename = "Affair", lastname = "Master", value = 4 }
};

var data = listName
    .Where(x => x.value != 0)
    .GroupBy(x => (x.firstname, x.middlename, x.lastname))
    .ToDictionary(x => x.Key, x => x.ToList());

var myfirstname = "David";
var mylastname = "Broom";
var matchingPeople = data.Where(x => x.Key.firstname == myfirstname && x.Key.lastname == mylastname).SelectMany(x => x.Value);

You need to use .Where(...) to filter the data, and .Select(...) to select the .Value part of the dictionary.您需要使用.Where(...)来过滤数据,并使用.Select(...)来选择字典的.Value部分。 Because .Value is a list, the result would be a List of List so use .SelectMany instead of .Select to aggregate these List into a single list .因为.Value是一个清单,其结果将是一个ListList ,以便使用.SelectMany代替.Select聚合这些List到一个单一的list

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

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