score:3

Accepted answer

1. you have mixed lambda expression with linq syntax. started with linq and end with lambda expression. try this

 var q = xmlDoc.Descendants("site").Where(tech => tech.Attribute("technical").Value == "true")
                   .Select(n => new { SiteName = n.Element("name").Value});

or you should be using the whole query in linq

var q = from c in xmlDoc.Descendants("site") where c.Attribute("technical").Value == "true"
                   select new { SiteName = c.Element("name").Value};

2. just use ToDictionary in the end of query

var q = xmlDoc.Descendants("site").Where(tech => tech.Attribute("technical").Value == "true")
                   .Select(n => new { SiteName = n.Element("name").Value,Key= n.Element("id").Value }).ToDictionary(n=>n.Key,l=>l.SiteName);

n=>n.Key will the key for dictionary and l=>l.siteName will be value.

3. There are two ways of create such queries one is using linq other is using methods that uses lambda expressions as parameter. both are easy, linq is somehow like SQL query while Methods use lambda are more like a short hand for manipulating data by description methods. i personally like method oriented way with lambda as its more sequentially readable.


Related Articles