没有 KEY 约束的字典或 KeyValuePair
我需要返回两个值的列表。 我尝试了字典,但第一个参数是关键,在我的情况下它会重复。 我的列表需要返回如下内容:
经理 - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
System.Tuple
class,例如:但是,创建自己的类有什么问题呢?它们几乎可以是自我记录的,特别是在像这样的简单情况下(例如,
Name
字段中的内容是显而易见的)。例如:旁注:我不知道您的应用程序域或您拥有的所有可能的角色/头衔,但如果您根据此值进行任何类型的处理,它看起来是使用
enum
代替。例如,You could use the
System.Tuple<T1, T2>
class, e.g,: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: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.,这又如何呢?
What about this?
试试这个:
您还可以使用类似
System.Web.UI.Pair
的东西(这不是通用的,但有时非常方便)。但只有当第一件事是位置而第二件事是名称确实无关紧要时才应该使用它。如果您想将其视为另一对某物,请使用 KeyValuePair 或 Pair。但如果它是位置和名称对的列表,那么为其创建一个特殊类型可能是个好主意。
Try this:
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.
.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 theToLookup
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 :)