In windows phone, I retrieved a table in database as dictionary in LINQ. My dictionary is like this
Dictionary<int, StudentTable>
I retrieved the table from LINQ as
var studentDictionary = studContext.StudentList.Select(p => new { p.RollNo, p }).AsEnumerable().ToDictionary(kvp => kvp.RollNo, kvp => kvp);
But I want to get all the values in the studentDictionary as a
List<StudentTable>
I know it is possible by adding each dictionary values to the list by using for loop.
How can I do this without a loop. ?
Is there any other better method than a loop?
Thanks
You can use studentDictionary.Values.
PS: The same works for the keys, using studentDictionary.Keys
Your dictionary contains Value which is an object of type StudentTable. You can select that from the dictionary and apply ToList method.
var list = studentDictionary.Values.ToList();
or
var list = studentDictionary.Select(r => r.Value).ToList();
Related
I have a dictionary with following Structure
Dictionary<string, List<string>>
I want to extract all the values of the above dictionary and pass as list of strings to another method.
like
MyMethod(List<string>)
Do we have an efficient way to do this without foreach loop?
Assuming you mean you want all values as a flat list, you can use LINQ:
var myDict = new Dictionary<string, List<string>>();
var allValues = myDict.Values.SelectMany(v => v).ToList();
.SelectMany will flatten the enumerable returned from the expression v => v.
If you're using .NET Core in Visual Studio, you might need to add using System.Linq; to the top of the file.
I have a dictionary of Guid, Asset and I'd like to get the names from a list of keys that I have:
Dictionary<Guid, Asset> assetDict;
List<Guid> selectedAssetGuidList; //subset of assetDict.Keys
From these Guid's I'd like to get all the Asset.name properties, namely a list of
assetDict[selectedAssetGuid].name
where selectedAssetGuid is one element in selectedAssetGuidList. Thanks Y'all
Method syntax:
var result = selectedAssetGuidList.Select(guid => assetDict[guid].name);
or query syntax:
var result = from guid in selectedAssetGuidList
select assetDict[guid].name;
Dictionary<int, List<Customer>> dictionary = new Dictionary<int, List<Customer>>();
I want to query based on the key and get a List back. Not sure how to structure the LINQ query for that.
Desired Output:
A List<Customer> for a particular key in the Dictionary.
That's what the Dictionary (as you've defined the generic arguments) will do. So, dictionary[key] will return the list. Note that it will throw an exception if you haven't initialized it already with dictionary[key] = new List<Customer>();.
You don't need to use LINQ for this, but if you really want to
int key = 1;
List<Customer> customers = dictionary.Single(item => item.Key == key).Value;
The simplest way is to just retrieve the value for the key using the regular [] operator
dictionary[key];
How to get values from nested Dictionary using LINQ. Keys in the Dictionary are integer. Values of Dictionary are list of lists. Seconday level list is a list of array holding integer value. Thanks in advance.
Dictionary<int, List<List<int[]>>> dictionary = ...
var values = dictionary.Values.SelectMany(x => x.SelectMany(y => y.SelectMany(z => z));
Edit: Didn't realize it was a list of lists, so modified to account for that.
I'm trying to loop over distinct values over a dictionary list:
So I have a dictionary of key value pairs .
How do I get just the distinct values of string keys from the dictionary list?
var distinctList = mydict.Values.Distinct().ToList();
Alternatively, you don't need to call ToList():
foreach(var value in mydict.Values.Distinct())
{
// deal with it.
}
Edit: I misread your question and thought you wanted distinct values from the dictionary. The above code provides that.
Keys are automatically distinct. So just use
foreach(var key in mydict.Keys)
{
// deal with it
}
Keys are distinct in a dictionary. By definition.
So myDict.Keys is a distinct list of keys.
Looping over distinct keys and doing something with each value...
foreach( dictionary.Keys )
{
// your code
}
If you're using C# 3.0 and have access to LINQ:
Just fetching the set of distinct values:
// you may need to pass Distinct an IEqualityComparer<TSource>
// if default equality semantics are not appropriate...
foreach( dictionary.Values.Distinct() )
{
}
If the dictionary is defined as:
Dictionary<string,MyType> theDictionary = ...
Then you can just use
var distinctKeys = theDictionary.Keys;
This uses the Dictionary.Keys property. If you need a list, you can use:
var dictionaryKeysAsList = theDictionary.Keys.ToList();
Since it's a dictionary, the keys will already be distinct.
If you're trying to find all of the distinct values (as opposed to keys - it wasn't clear in the question) in the dictionary, you could use:
var distinctDictionaryValues = theDictionary.Values.Distinct(); // .ToList();