简体   繁体   中英

Linq to object Duplication delete

I have codes like this;

XElement TEST = XElement.Parse(
@"<SettingsBundle>
    <SettingsGroup Id=""QAVerificationSettings"">
        <Setting Id=""RegExRules"">True</Setting>
        <Setting Id=""RegExRules1""><RegExRule><Description>Foo</Description><IgnoreCase>false</IgnoreCase><RegExSource></RegExSource><RegExTarget>[^\s]+@[^\s]+</RegExTarget><RuleCondition>TargetOnly</RuleCondition></RegExRule></Setting>
        <Setting Id=""RegExRules2""><RegExRule><Description>Boo</Description><IgnoreCase>false</IgnoreCase><RegExSource></RegExSource><RegExTarget>\s{2,}</RegExTarget><RuleCondition>TargetOnly</RuleCondition></RegExRule></Setting>
        <Setting Id=""RegExRules2""><RegExRule><Description>Boo</Description><IgnoreCase>false</IgnoreCase><RegExSource></RegExSource><RegExTarget>\s{2,}</RegExTarget><RuleCondition>TargetOnly</RuleCondition></RegExRule></Setting>
    </SettingsGroup>
</SettingsBundle>");
List<XElement> LIST1 = new List<XElement> { };
foreach( XElement x in TEST.Descendants( "RegExRule"))
    LIST1.Add(x);
var LIST2 = LIST1.Distinct();           
var NEW = new XDocument(
    new XElement("SettingsBundle",
        new XElement("SettingsGroup", new XAttribute("Id", "QAVerificationSettings"),
            new XElement("Settings", new XAttribute("Id", "RegExRules"), "True"),
            LIST2.Select((x, i) => new XElement("Setting", new XAttribute("Id", "RegExRules" + i ), x ))
    )));
NEW.Save(Directory.GetCurrentDirectory() + @"\_Texting.xml");

I'd like to delete one of the same item (I need only one "RegExRules2" not two).

But, have failed.

What do I have to do ? (I guess, I am not good at with "Distinct()")

Thanks

You can do GroupBy and use only the first item in each group to get distinct elements :

XNamespace ns = "http://schemas.datacontract.org/2004/07/Sdl.Verification.QAChecker.RegEx";

var distinctRules = TEST.Descendants(ns + "RegExRule")
                        .GroupBy(o => o.ToString())
                        .Select(o => o.First());
var result = new XDocument(
    new XElement("SettingsBundle",
        new XElement("SettingsGroup", new XAttribute("Id", "QAVerificationsettings"),
            new XElement("Settings", new XAttribute("Id", "RegExRules"), "True"),
            distinctRules.Select((x, i) => new XElement("Setting", new XAttribute("Id", "RegExRules" + i), x))
    )));

result.Save(Directory.GetCurrentDirectory() + @"\_Texting.xml");

Another possibility would be to create an IEqualityComparer for XElement .

A really basic example (which you shouldn't treat as a good example for an implementation of GetHashCode . Look here for that ) would be:

public class XElementComparer : IEqualityComparer<XElement>
{

    public bool Equals(XElement x, XElement y)
    {
        return x.Parent.Attribute("Id").Value == y.Parent.Attribute("Id").Value;
    }

    public int GetHashCode(XElement obj)
    {
        string val = obj.Parent.Attribute("Id").Value;
        return val.GetHashCode();
    }
}

And in your Distinct operation would afterwards look like this: var LIST2 = LIST1.Distinct(new XElementComparer());

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM