Skip to main content

Microsoft Silverlight

Answered Question Select Distinct Values from XML using LINQRSS Feed

(0)

neal.gabriel
neal.gab...

Participant

Participant

789 points

161 Posts

Select Distinct Values from XML using LINQ

Hi All,

Is it possible to retrive distinct values from XML ?

The sscenario is as follows

I have Class called TestClass which looks like this

public class TestClass

{

public int ApplicationID { get; set; }

public string UserID { get; set; }

public string Folder { get; set; }

public string Status { get; set; }

public string Read { get; set; }

}

I ll load an XML which should be filtered;

Here  is the code which I have written to load XML to the Class

var SearchResults = from doc in docsInBox.Descendants("XMLROOT")

where Convert.ToBoolean((doc.Element("SOMEBOOLVALUE")).Value.Trim()) == false

orderby Convert.ToDateTime((doc.Element("CreatedDate")).Value.Trim()) descending

select new TestClass

{

ApplicationID = (
int)(doc.Element("ApplicationID")),

UserID = ((string)(doc.Element("UserID")) == null ? "" : (doc.Element("UserID")).Value.Trim()),

Folder = ((string)(doc.Element("Folder")) == null ? "" : (doc.Element("Folder")).Value.Trim()),

Status = ((string)(doc.Element("Status")) == null ? "" : (doc.Element("Status")).Value.Trim()),

Read = ((string)(doc.Element("Read")) == null ? "" : (doc.Element("Read")).Value.Trim())

};

In the Variable SearchResults, I ll get the results which has Duplicate Rows.

Is it possible to write a distinct code in the above code, or do we need to write it saperately,

I tried a test which will retrieve only one value, which is as follows

var TempSearchResults = (from oSearchResults in SearchResults

select oSearchResults.ApplicationID).Distinct().ToList();

Please try to find me a  solution to get the distinct elements from an XML;

 

Regards

Neal Gabriel

Regards...

Neal Gabriel

amyo
amyo

Contributor

Contributor

3620 points

495 Posts

Answered Question

Re: Select Distinct Values from XML using LINQ

In your scenario you can select from xml with duplicate and then distinct the selected element as you mentioned in your second solution but with little modification.

You need to implement the IEqualityComparer interface and then use it on distinct operation.

Sample:

            var testClassList = new List()
            {
                new TestClass{ApplicationID=1,Folder="f1",Read="r1"},
                new TestClass{ApplicationID=1,Folder="f1",Read="r1"},
                new TestClass{ApplicationID=2,Folder="f1",Read="r1"},
                new TestClass{ApplicationID=3,Folder="f1",Read="r1"},
                new TestClass{ApplicationID=1,Folder="f2",Read="r1"},
                new TestClass{ApplicationID=2,Folder="f1",Read="r1"},
            };
            var aa = testClassList.Distinct(new MyComparer());
 
    public class MyComparer:IEqualityComparer<TestClass>
    {
        #region IEqualityComparer Members

        public bool Equals(TestClass x, TestClass y)
        {
            if (x == null || y == null)
                return false;
            else
                return x.ApplicationID == y.ApplicationID;
        }

        public int GetHashCode(TestClass obj)
        {
            return obj.ApplicationID.GetHashCode();
        }

        #endregion
    }
 Hope this will help you.

Please Mark as Answer if this helps you.

Amyo Kabir
Solution Architect & Sr. Developer
Blog

neal.gabriel
neal.gab...

Participant

Participant

789 points

161 Posts

Re: Re: Select Distinct Values from XML using LINQ

That Worked! Thanks a Lot Dude

Smile

Regards...

Neal Gabriel
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities