【问题标题】:nested Array from LINQ c#来自 LINQ c# 的嵌套数组
【发布时间】:2013-12-04 06:15:47
【问题描述】:

我有以下集合类

public class Customer
{
        public int Id{get;set}
        public DateTime StartTime { get; protected set; }
        public IList<ShoppingList> shoppingList{ get; protected set; }

}

public class shoppingList
{
    public int price{get;set;}
    public int Id{get;set;}
    public string name {get;set;}
}

现在我有列表客户

如何将其转换为嵌套数组,以便获取每个客户的购物清单。

【问题讨论】:

    标签: c# arrays linq datetime virtual


    【解决方案1】:

    试试这个:

    var customers = listcustomers.Select(c=>c.shoppingList.ToArray())
                                 .ToArray();
    //get shoppingList array of customer 0
    customers[0];
    //get the first shoppingList of customer 0
    customers[0][0];
    

    请注意,这样做不会跟踪客户的信息,您只知道该客户的客户索引和对应的shoppingList

    【讨论】:

    • 是的,这是真的。但我不想保留客户类型。我有一个返回 double[][] 的函数。例如返回新的 []{}..
    • 是的,您所做的当然返回了 Customer[][],这是正确的。但我需要它返回的是返回 double[][],没有强制转换:)
    • @JOY double[][]实际上只能存储doubles,那么你想要什么实际值呢?你的问题很混乱
    • 好吧,这是我期望的结果之王,为购物清单增加价格 var points = [ [[2,5], [30,60], [50, 20]], [[2, 5], [30,60], [50, 20]]];
    • @KingKing 我认为@JOY 的意思是c.shoppingList.Select(s=&gt;s.Price).ToArray()
    【解决方案2】:

    这是你想要的吗?

     var points= listcustomers.Select(x => x.shoppingList
                                            .Select(shop => new[] { 
                                                             shop.Id, 
                                                             shop.price })).ToArray();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多