Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ select in C# dictionary

I have next dictionary in C#

Dictionary<string, object> subDictioanry = new Dictionary<string, object>();  List<Dictionary<string, string>> subList = new List<Dictionary<string, string>>();  subList.Add(new Dictionary<string, string>(){     {"valueLink", "link1"},     {"valueTitle","title1"} }); subList.Add(new Dictionary<string, string>(){     {"valueLink", "link2"},     {"valueTitle","title2"} }); subList.Add(new Dictionary<string, string>(){     {"valueLink", "link3"},     {"valueTitle","title3"} });  subDictioanry.Add("title", "title"); subDictioanry.Add("name", "name"); subDictioanry.Add("fieldname1", subList);  Dictionary<string, object> exitDictionary = new Dictionary<string, object>(); exitDictionary.Add("first", subDictioanry); exitDictionary.Add("second", subDictioanry); 

Is it possible to get all "valueTitle" with help of LINQ select?

UPDATE: Sorry, i should write it first - i need to get result from exitDictionary, not from subList

like image 386
user2944829 Avatar asked Nov 01 '13 11:11

user2944829


People also ask

What is select in LINQ?

LINQ Select comes under the Projection Operator, the select operator used to select the properties to display/selection. Select operator is mainly used to retrieve all properties or only a few properties which we need to display. It is used to select one or more items from the list of items or from the collection.

How do I select a query in LINQ?

LINQ query syntax always ends with a Select or Group clause. The Select clause is used to shape the data. You can select the whole object as it is or only some properties of it. In the above example, we selected the each resulted string elements.

What does select in LINQ return?

The Select() method invokes the provided selector delegate on each element of the source IEnumerable<T> sequence, and returns a new result IEnumerable<U> sequence containing the output of each invocation.

What is LINQ used for in C?

LINQ provides functions to query cached data from all kinds of data sources. The data source could be a collection of objects, database or XML files.


1 Answers

If you are searching by the fieldname1 value, try this:

var r = exitDictionary    .Select(i => i.Value).Cast<Dictionary<string, object>>()    .Where(d => d.ContainsKey("fieldname1"))    .Select(d => d["fieldname1"]).Cast<List<Dictionary<string, string>>>()    .SelectMany(d1 =>         d1         .Where(d => d.ContainsKey("valueTitle"))         .Select(d => d["valueTitle"])         .Where(v => v != null)).ToList(); 

If you are looking by the type of the value in the subDictionary (Dictionary<string, object> explicitly), you may do this:

var r = exitDictionary    .Select(i => i.Value).Cast<Dictionary<string, object>>()    .SelectMany(d=>d.Values)    .OfType<List<Dictionary<string, string>>>()    .SelectMany(d1 =>         d1         .Where(d => d.ContainsKey("valueTitle"))         .Select(d => d["valueTitle"])         .Where(v => v != null)).ToList(); 

Both alternatives will return:

title1 title2 title3 title1 title2 title3 
like image 142
Alex Filipovici Avatar answered Sep 19 '22 17:09

Alex Filipovici