简体   繁体   中英

Select All object values to list from dictionary

I have a DTO like this:

public class OrderDraft
{
    public string FTPUser             { get; set; }
    public string AccountRef          { get; set; }
    public string SupplyFromWarehouse { get; set; }
    public string UsePriceband        { get; set; }

    public string DocumentDate        { get; set; }
    public string DateRequested       { get; set; }
    public string DatePromised        { get; set; }
    public string CustomerDocumentNo  { get; set; }

    public List<Line> Lines           { get; set; }

    public Address DeliveryAddress    { get; set; }
    public string ShippingChargeCode  { get; set; }
    public decimal ShippingFee        { get; set; }
}

I create a dictionary of the above like this:

Dictionary<string, OrderDraft> multiOrders 
    = new Dictionary<string, OrderDraft>();

Now I want to return a List<OrderDraft> from the above multiOrders dictionary. To achieve this, I tried the following:

  • return multiOrders.SelectMany(s => s.Value);
  • return multiOrders.SelectMany(s => s.Value).ToList<OrderDraft>;

I am getting the following error:

Error 2 The type arguments for method 'System.Linq.Enumerable.SelectMany(System.Collections.Generic.IEnumerable, System.Func>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Any idea how to retrieve a List<T> of all values from a Dictionary<String, T> ?

怎么样:

multiOrders.Values.ToList() 

@nvoigt解决方案已经死了,但您也可以通过明确声明类型参数来处理警告:

return multiOrders.SelectMany<OrderDraft,OrderDraft>(s => s.Value).ToList<OrderDraft>;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM