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>
?
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.
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.
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.
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.
How about:
multiOrders.Values.ToList()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With