当按一对列分组时,如何以列表形式获取一组值

发布于 2024-12-07 07:02:49 字数 121 浏览 2 评论 0原文

我有一个包含以下值的数据表

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 技术交流群。

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

发布评论

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

评论(3

坚持沉默 2024-12-14 07:02:49

我建议您创建一个封装所有数据的类,然后您可以创建适当类型的 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 the DataTable.

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?)

淡看悲欢离合 2024-12-14 07:02:49

如果没有使用上下文,很难回答。这是否会以正确的方式使用,并与系统的其他部分进行通信。下面假设它不与系统的其他部分通信

var list = (from e in DataTable.Rows.AsEnumerable()
            select new {
               id = e["id"],
               Name = e["Name"], 
               data = e["data"]
             }).ToList()

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

var list = (from e in DataTable.Rows.AsEnumerable()
            select new {
               id = e["id"],
               Name = e["Name"], 
               data = e["data"]
             }).ToList()
演出会有结束 2024-12-14 07:02:49

创建一个映射到您的表上的类。您可以使用 EntityFramework、LINQ to SQL、nHibernate 或自定义 ORM 从表中检索数据作为这些对象。选择对象并使用 LINQ 分组运算符创建匿名对象(或具有日期列表的另一个类)。

public class Foo
{
   public int ID { get; set; }
   public sring Name { get; set; }
   public DateTime Date { get; set;
}

public class Bar
{
   public int ID { get; set; }
   public string Name { get; set; }
   public List<DateTime> Dates { get; set; }
}

public class FooDataContext : DbContext
{
   IDbSet<Foo> Foos { get; set; }
}

using (var context = new FooDataContext())
{
     List<Bar> bars = context.Foos
                             .GroupBy( f => new { f.ID, f.Name } )
                             .Select( g => new Bar
                              {
                                  ID = g.Key.ID,
                                  Name = g.Key.Name,
                                  Dates = g.Select( f => f.Date )
                              });
}

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

public class Foo
{
   public int ID { get; set; }
   public sring Name { get; set; }
   public DateTime Date { get; set;
}

public class Bar
{
   public int ID { get; set; }
   public string Name { get; set; }
   public List<DateTime> Dates { get; set; }
}

public class FooDataContext : DbContext
{
   IDbSet<Foo> Foos { get; set; }
}

using (var context = new FooDataContext())
{
     List<Bar> bars = context.Foos
                             .GroupBy( f => new { f.ID, f.Name } )
                             .Select( g => new Bar
                              {
                                  ID = g.Key.ID,
                                  Name = g.Key.Name,
                                  Dates = g.Select( f => f.Date )
                              });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文