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 Query

More Query from same tag