Last time I ask about linq, how to get information about element from my database if there is possibility that object exist or not. The answer was:
var elements = XElement.Load("objects.xml");
var query1 = from query in elements.Descendants("Lemma")
let null_LemmaSign = query.Element("Lemma.LemmaSign")
et null_TE = query.Descendants("TE.TE").FirstOrDefault()
where wyszuk == query.Element("Lemma.LemmaSign").Value
select new
{
word = null_LemmaSign == null ? "none" : null_LemmaSign.Value,
te = null_TE == null ? "none" : null_TE.Value,
};
foreach (var e in query1)
{
MessageBox.Show(e.word.ToString() + " - " + e.te.ToString());
}
Other possibility was
let null_TE = query.Element("Sense").Element("TE").Element("TE.TE")
But now I have situation that I have to get more than first element of <TE>
.
Example from database (now I can pick only first item, cat, but I wish to collect them all)
<TE><TE.TE> cat</TE.TE></TE>,<TE><TE.TE> cat2</TE.TE></TE>,<TE><TE.TE> cat3</TE.TE></TE>
Use .ToList()
instead of the .FirstOrDefault()
. The second line in the select
clause will have to change since you will be dealing with a list instead of a single element
Note that the list will never be null but it could contain 0 items.
You want to use .Take() and use the number of elements you want
Also useful function is .Skip()
so it is easy to do something like paging for instance with a combination of them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.