当按一对列分组时,如何以列表形式获取一组值
我有一个包含以下值的数据表
id Name date
1 a 5/3/2011
1 a 6/4/2011
我想检索每个 id/name 对的值以及关联日期列表。
I have a datatable with below values
id Name date
1 a 5/3/2011
1 a 6/4/2011
I want to retrieve the values with a list of associated dates for each id/name pair.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(3)
我建议您创建一个封装所有数据的类,然后您可以创建适当类型的
List
。您将为DataTable
中的每个条目创建一个新类的实例。如果您使用强类型数据集,您可以根据需要使用生成的 DataRow 类型。
(不清楚“作为单个条目存储在列表中”是什么意思 - 整个表,还是每行一个条目?)
I would suggest you create a class which encapsulates all of that data, and then you can create a
List<T>
of the appropriate type. You'd create an instance of your new class per entry in theDataTable
.If you use a strongly-typed dataset you could use the generated
DataRow
type instead, if you wanted.(It's not clear what you mean by "store in a list as single entry" - the whole table, or one entry per row?)
如果没有使用上下文,很难回答。这是否会以正确的方式使用,并与系统的其他部分进行通信。下面假设它不与系统的其他部分通信
It's difficult to answer with out the context of the usage. Is this going to be used right a way, communicated to other parts of the system. The below assumes that it's not coomunicated to other parts of the system
创建一个映射到您的表上的类。您可以使用 EntityFramework、LINQ to SQL、nHibernate 或自定义 ORM 从表中检索数据作为这些对象。选择对象并使用 LINQ 分组运算符创建匿名对象(或具有日期列表的另一个类)。
Create a class that maps onto your table. You can use EntityFramework, LINQ to SQL, nHibernate or a custom ORM to retrieve data from the table as these objects. Select the objects and use the LINQ grouping operator to either create anonymous objects (or another class with the list of dates).