开发者

How can I access this in C#?

private void LoadSubforums(Forum forum, XElement subforumsXML)
{
    forum.Subforums = (from forumItem in subforumsXML.Descendants("forum")
                      select new Subforum()
                      {
                          ID = (string)forumItem.Element("id"),
                          Name = (string)forumItem.Element("name"),
                          URL = (string)forumItem.Element("url"),
                          Description = (string)forumItem.Element("description"),
                          Type = (string)forumItem.Element("type"),
                          TopicCount = (string)forumItem.Element("topics"),
                          ReplyCount = (string)forumItem.Element("replies"),
                          LastPost = ParseLastPost(forumItem.Element("lastpost")),
                          IsRedirect = How can I access the LastPost attribute here? :S
                      }).ToList();
}
}

The IsRedirect attribute of the Subforum class is dependant of a value inside the LastPost attribute.

Is it possible for me to access this attribute from within the Linq query?

EDIT:

This is what I'm trying to do:

forum.Subforums = (from forumItem in subforumsXML.Descendants("forum")
                               let lastPost = ParseLastPost(forumItem.Element("lastpost"))
                               select new Subforum()
                               {
                                   ID = (string)forumItem.Element("id"),
                                   Name = (string)forumItem.Element("name"),
                                   URL = (string)forumItem.Element("url"),
                                   Description = (string)forumItem.Element("description"),
                                   Type = (string)forumItem.Element("type"),
                                   TopicCount = (string)forumItem.Element("topics"),
                                   ReplyCount = (string)forumItem.Element("replies"),
                                   LastPost = lastPost,
                                   IsRedirect = if(lastPost.ID == null) ? true ; false
                开发者_开发技巧               }).ToList();

But this code doesn't compile.


forum.Subforums = (from forumItem in subforumsXML.Descendants("forum")
                   let lastPost = ParseLastPost(forumItem.Element("lastpost"))
                   select new Subforum()
                   {
                       ID = (string)forumItem.Element("id"),
                       Name = (string)forumItem.Element("name"),
                       URL = (string)forumItem.Element("url"),
                       Description = (string)forumItem.Element("description"),
                       Type = (string)forumItem.Element("type"),
                       TopicCount = (string)forumItem.Element("topics"),
                       ReplyCount = (string)forumItem.Element("replies"),
                       LastPost = lastPost,
                       IsRedirect = !string.IsNullOrEmpty(lastPost)
                   }).ToList();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜