【问题标题】:How can I convert each record from query result to arraylist c#如何将每条记录从查询结果转换为arraylist c#
【发布时间】:2013-07-04 19:28:57
【问题描述】:

抱歉,我是 C# 新手,我想将从 DATABASE 检索到的每条记录转换为 arraylist 我的代码如下:

var reservation = from x in db.resrvation \
                  where x.date=Calendar1.SelectedDate 
                  select x;

在这里,我想获取保留中的每条记录并将其转换为数组,以便我可以获取特定数据,例如客户名称

通过保留检索到的集合包括 no。每条记录都有 clintName、Phone、ReservationDate 等属性。我想为每条记录创建一个数组列表。

实际上我正在尝试使用这个数组列表来填充原始数据表。

【问题讨论】:

  • 首先 - 不要使用 ArrayList,因为它不是强类型的。其次 - 如果您正在查询预订,那么 rx 更适合查询变量的名称。最后 - 如果您要返回列表,那么 reservations 是比单个 reservation 更合适的名称
  • 感谢你的提示lazyberezovsky :)

标签: c# linq arraylist


【解决方案1】:

只需转换为 List 即可。

var reservations = (from x in db.resrvation 
                   where x.date=Calendar1.SelectedDate 
                   select x).ToList();

【讨论】:

  • 感谢 4 您的快速重播 - 目标是为每个预留获取一个数组列表,而不是数组列表包含所有预留,因此我可以逐个单元格填充数据表...我尝试了 .toList 但它将给出一个包含所有预订的列表
  • @user2278268:为什么数组列表不包含所有保留?我不明白这个问题。请多解释一下。谢谢
  • 如果它是arraylist包含所有保留,我可以从对象中检索特定细节
  • 是的,你应该这样做。你想达到什么细节,如果有任何问题,请告诉它。
  • 例如,我想检索第一次预订的 clintName
【解决方案2】:

听起来您想为每一行选择一个 ArrayList。试试这个:

var reservation = from r in db.reservation
    where r.date == Calendar1.SelectedDate
    select new ArrayList(new object[] { r.name, r.whatever });

【讨论】:

    【解决方案3】:

    好吧,要将其转换为 Arraylist,您可以这样做:

    (from x in db.resrvation where x.date=Calendar1.SelectedDate select x).ToArrayList();
    

    假设你有这个扩展方法:

    public static ToArrayList(this IEnumerable source)
    {
        var a = new ArrayList();
        foreach (object item in source) a.Add(item);
        return a;
    }
    

    但是,我会改用通用类型的 List<T>,如下所示:

    (from x in db.resrvation where x.date=Calendar1.SelectedDate select x).ToList();
    

    【讨论】:

    • 谢谢 4 您的快速重播-目标是为每个预留获取一个数组列表,而不是数组列表包含所有预留,因此我可以逐个单元格填充数据表...我尝试了 .toList 但它将给出一个包含所有预订的列表
    猜你喜欢
    • 1970-01-01
    • 2020-03-25
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多