没有 KEY 约束的字典或 KeyValuePair

发布于 2024-11-09 17:24:20 字数 265 浏览 0 评论 0原文

我需要返回两个值的列表。 我尝试了字典,但第一个参数是关键,在我的情况下它会重复。 我的列表需要返回如下内容:

经理 - John Zambers

开发人员 - Paul Myers

开发人员 - John Zambers

测试人员 - Mary Marie

等... - 等等...

我不想创建自己的类来表示此列表。 Dot.Net 有一个内置的 Type 来代表它吗?有点像List()?

提前致谢。

I need to return a list of two values.
I tried Dictionary but the first parameter is the key and in my case it will repeat.
My list needs to return something like this:

Manager - John Zambers

Developer - Paul Myers

Developer - John Zambers

Tester - Mary Marie

etc... - etc...

I dont want to create my own class to represent this list.
Dot.Net have a built in Type that represent it? Somethink like List()?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

抹茶夏天i‖ 2024-11-16 17:24:20

您可以使用 System.Tuple class,例如:

List<Tuple<String, String>> people = new List<Tuple<String, String>>();
people.Add(Tuple.Create("Manager", "John Zambers"));
// etc.

但是,创建自己的类有什么问题呢?它们几乎可以是自我记录的,特别是在像这样的简单情况下(例如,Name 字段中的内容是显而易见的)。例如:

public class Person
{
   public string Role { get; set; }
   public string Name { get; set; }
}

旁注:我不知道您的应用程序域或您拥有的所有可能的角色/头衔,但如果您根据此值进行任何类型的处理,它看起来是使用 enum 代替。例如,

public enum Roles
{
    Manager,
    Developer,
    Tester
}

You could use the System.Tuple<T1, T2> class, e.g,:

List<Tuple<String, String>> people = new List<Tuple<String, String>>();
people.Add(Tuple.Create("Manager", "John Zambers"));
// etc.

However, what's wrong with creating your own class? They can be almost self-documenting, especially in a simple case like this (e.g, it's obvious what's going to be in the Name field). For example:

public class Person
{
   public string Role { get; set; }
   public string Name { get; set; }
}

Side note: I don't know your application domain or what all the possible roles/titles you have, but if you're doing any sort of processing based on this value it looks like a good place to use an enum instead. E.g.,

public enum Roles
{
    Manager,
    Developer,
    Tester
}
多像笑话 2024-11-16 17:24:20

这又如何呢?

Dictionary<string, List<string>> role_names = new Dictionary<string, List<string>>();
List<string> lst;

role_names.Add("Manager", lst = new List<string>());
lst.Add("John Zambers");

role_names.Add("Developer", lst = new List<string>());
lst.Add("Paul Myers");
lst.Add("John Zambers");

role_names.Add("Tester", lst = new List<string>());
lst.Add("Mary Marie");

What about this?

Dictionary<string, List<string>> role_names = new Dictionary<string, List<string>>();
List<string> lst;

role_names.Add("Manager", lst = new List<string>());
lst.Add("John Zambers");

role_names.Add("Developer", lst = new List<string>());
lst.Add("Paul Myers");
lst.Add("John Zambers");

role_names.Add("Tester", lst = new List<string>());
lst.Add("Mary Marie");
浅浅 2024-11-16 17:24:20

试试这个:

List<KeyValuePair<string,string>> list = new List<KeyValuePair<string,string>>();
list.Add(new KeyValuePair<string, string>("Manager", "John"));
list.Add(new KeyValuePair<string, string>("Manager", "John"));
Console.WriteLine(list[0].Key);

您还可以使用类似 System.Web.UI.Pair 的东西(这不是通用的,但有时非常方便)。

但只有当第一件事是位置而第二件事是名称确实无关紧要时才应该使用它。如果您想将其视为另一对某物,请使用 KeyValuePair 或 Pair。但如果它是位置和名称对的列表,那么为其创建一个特殊类型可能是个好主意。

Try this:

List<KeyValuePair<string,string>> list = new List<KeyValuePair<string,string>>();
list.Add(new KeyValuePair<string, string>("Manager", "John"));
list.Add(new KeyValuePair<string, string>("Manager", "John"));
Console.WriteLine(list[0].Key);

You can also use something like System.Web.UI.Pair (which is not generic but pretty convenient sometimes).

But you should use it only if it really doesn't matter that first thing is a position and the second is a name. If you want to treat it as another one pair of something -- use KeyValuePair or Pair. But if it's a list of position and name pair, it might be a good idea to make a special type for it.

漫雪独思 2024-11-16 17:24:20

.NET确实有一个用于此类事情的类,但您不能直接使用它。它是 Lookup,由 ToLookup 返回LINQ 中的 方法。

如果您能想出一种方法,在您拥有的任何数据源上使用 ToLookup,那么您的生活就会轻松很多。

您始终可以使用我的 Lookup 实现Edulinq的代码>。基本上,这两种实现在程序集本身之外都是不可变的,但是如果您愿意,您可以将我的实现更改为可变的:)

.NET does have a class for this sort of thing, but you can't use it directly. It's Lookup, and is returned by the ToLookup method in LINQ.

If you can work out a way to use ToLookup on whatever source you've got for your data, it'll make your life a lot easier.

You could always use my implementation of Lookup from Edulinq if you wanted. Basically both implementations are immutable from outside the assembly itself, but you could change mine to be mutable if you wanted :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文