开发者

equivalent lambda expression for this query expression

As per post Select element with given attribute using linq to xml what will be the equivalent lambda expression.

The below solution works fine

var artistsAndImage = from a in feed.Descendants("artist")
                      from img in a.Elements("image")
                      where img.Attribute("size").Value == 开发者_JS百科"big"
                      select new { Name = a.Element("Name").Value
                                 , Image = img.Value};

I tried the lambda expression but it's not working :-( can somebody suggest the equivalent lambda expression.


Sure:

var artistsAndImage = feed.Descendants("artist")
                          .SelectMany(a => a.Elements("image"),
                                      (a, img) => new { a, img })
                          .Where(z => z.img.Attribute("size").Value == "big")
                          .Select(z => new { Name = z.a.Element("Name").Value,
                                             Image = z.img.Value });

(Untested, but I think it should work.)

The tricky bit here is that the second from clause calls SelectMany and introduces a transparent identifier which I've made somewhat less transparent by calling it z.

Any particular reason you want to avoid query expression syntax here though? It's simpler in this example - I just use whichever is simpler for the query I'm writing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜