Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

using where clause in linq to XML

$
0
0

Dear all,

I have the followinf code snipet

public List<Answer> GetAnswers(System.Guid QuestionId,XDocument doc)
        {
                List<Answer> A = new List<Answer>();
             var a = (from answer in doc.Descendants("Answer") select answer).ToList();
                if (a != null)
                {
                    foreach (var item in a)
                    {
                        if (item.Attribute("QuestionId").Value.Equals(QuestionId.ToString(), StringComparison.InvariantCultureIgnoreCase))
                        {
                            Answer answer = new Answer();
                            answer.Id = new Guid(item.Attribute("Id").Value);
                            answer.IsCorrectAnswer = (bool)item.Attribute("IsCorrectAnswer");
                            answer.Order = 0;
                            answer.QuestionId = QuestionId;
                            answer.Title =item.Element("Title").Value;

                            A.Add(answer);
                        }
	                }
                    return A;
                }
            return null;
        }

By doing it as shown below, in the from statement I am getting all items and then filter them afterwards within the IF statement. How can I remove the if statement and place it in as a where clause inside the ling to XML ?

When I try it, I get a null object exception

Thnaks for help

regards


Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>