Linq to xml select most frequent value
I need to select the most frequest occurrence of an value in a linq to xml collection of elements. How do you do that?
Edit, Here is what i tried but its obviously not correct;
XDocument btC开发者_如何学Goheck = XDocument.Load("https://www.url.com" + postcode);
var districtCode = btCheck.Descendants("DSL_CHECKER").Elements("ADDRESS_DETAILS").Elements("ADDRESS_DETAIL").Elements("ADDRESS").Elements("DISTRICTID");
string d = (districtCode.GroupBy(z => z.Value).OrderBy(z => z.Key).Take(1)).First();
As you don't show what you tried so far, I will just give some hints instead of a full answer:
GroupBy
and then Max
.
var code = btCheck.Descendants("ADSL_CHECKER").Elements("ADDRESS_DETAILS")
.Elements("ADDRESS_DETAIL").Elements("ADDRESS").Elements("DISTRICTID")
.GroupBy(z => z.Value).Select(group => new
{
value = group.Key,
Count = group.Count()
})
.OrderByDescending(z => z.Count).FirstOrDefault();
精彩评论