Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 886
Latheesan Avatar asked May 06 '14 08:05

Latheesan


People also ask

How can I get a list of values from a dictionary?

Get a list of values from a dictionary using List comprehension. Using List comprehension we can get the list of dictionary values. Here we are using a list comprehension to iterate in a dictionary by using an iterator. This will return each value from the key: value pair.

How do you get all values from a Python dictionary?

Python has a built-in method called values() that returns a view object. The dictionary's values are listed in the view object. We can use the values() method in Python to retrieve all values from a dictionary.

How do I extract all the values of specific keys from a list of dictionaries?

Method 2: Extract specific keys from the dictionary using dict() The dict() function can be used to perform this task by converting the logic performed using list comprehension into a dictionary.

Can dictionary values be a list?

It definitely can have a list and any object as value but the dictionary cannot have a list as key because the list is mutable data structure and keys cannot be mutable else of what use are they.


1 Answers

How about:

multiOrders.Values.ToList() 
like image 144
nvoigt Avatar answered Nov 06 '22 08:11

nvoigt