【问题标题】:C# Linq XML GroupBy OrderBy get countC# Linq XML GroupBy OrderBy 获取计数
【发布时间】:2016-08-18 23:26:14
【问题描述】:

我有这个 xml 结构,我需要非空工作和家庭元素的计数,groupby Country 属性,然后 orderby country 属性。

类似于 Dictionary> 的东西,但我在 linq 表达式上失败了。目前该列表仅被过滤,groupby 和 orderby。缺少的部分是计数非空工作和家庭元素。 是否可以在查询中立即执行或应该在查询列表后使用?

XML 文件

<?xml version="1.0" encoding="utf-8"?>
<Root>
    <Region ID="NA">
        <Name>QWERTZ</Name>
        <Country ID="1">
            <Facility Name="1">
                <Department Name="1">
                    <Member Name="1">
                        <Home>555-666</Home>
                        <Work>111-666</Work>
                    </Member>
                </Department>
            </Facility>
        </Country>      
    </Region>
        <Region ID="CHINA">
        <Name>ASDF</Name>
        <Country ID="2">
            <Facility Name="2">
                <Department Name="2">
                    <Member Name="2">
                        <Home>111-222</Home>
                        <Work>111-222</Work>
                    </Member>
                </Department>
            </Facility>
        </Country>      
    </Region>
    <Region ID="EU">
        <Name>ASDF</Name>
        <Country ID="3">
            <Facility Name="3">
                <Department Name="1">
                    <Member Name="1">
                        <Home>111-222</Home>
                        <Work></Work>
                    </Member>
                </Department>
            </Facility>
        </Country>      
    </Region>
</Root>

方法

 public static List<IGrouping<string,XElement>> getgrouped(string filename)
{
    XDocument xml = XDocument.Load(filename);

    return xml.Root
        .Descendants("Country")
        .GroupBy(x => (string)x.Attribute("ID").Value)
        .OrderBy(g => g.Key)
        .ToList();
}

【问题讨论】:

    标签: c# xml linq


    【解决方案1】:

    对于每个 Descendant,我使用国家 ID 和非空 Descendant WorkHome 元素的数量投影一个对象。然后我GroupBy 并总结了计数器。

    xml.Root.Descendants("Country")
        .Select(element => new 
        {
            Id = (string) element.Attribute("ID").Value,
            NonEmptyWork = element.Descendants("Work")
                                  .Count(w => !string.IsNullOrEmpty(w.Value)),
            NonEmptyHome = element.Descendants("Home")
                                  .Count(w => !string.IsNullOrEmpty(w.Value))
        })
        .GroupBy(item => item.Id)
        .Select(g => new
        { 
            Id = g.Key, 
            NonEmptyWorkAmount = g.Sum(item => item.NonEmptyWork), 
            NonEmptyHomeAmount = g.Sum(item => item.NonEmptyHome)
        })
        .OrderBy(item => item.Id)
        .ToList();
    

    将其输出为字典而不是ToList

        // Dictionary<string, YourType>
        .ToDictionary( key => key.Id, value => value);
    

    【讨论】:

    • Id = (string)x.Attribute("ID").Value X 未知,.Count(w => !string.IsNullOrEmpty(w.InnerText)) w.innertext 也是未知的,但你给出了正确的理解方向。
    • 必须找到解决按钮,但我还有一个与您的解决方案相关的问题。如果我想选择 ,那么我会因为保护级别而无法访问,但是该类是公共的,并且属性是公共的 get 和 set
    • @Shazter - 关于标记已解决,然后在答案的投票数下有一个灰色的 V 可以按下(变为绿色)。关于第二个问题,我将不得不查看一些代码-而且...后续问题应作为单独的问题发布。如果您无法解决该错误,请发布问题,如果您想在评论中标记我,我会尽力帮忙的:)
    猜你喜欢
    • 2013-05-28
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多