Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Select Distinct Values from XML using LINQ
2 replies. Latest Post by neal.gabriel on December 26, 2008.
(0)
neal.gab...
Participant
789 points
161 Posts
12-26-2008 12:49 AM |
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
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
{
UserID = ((
Status = ((
Read = ((
};
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
Please try to find me a solution to get the distinct elements from an XML;
Regards
Neal Gabriel
amyo
Contributor
3620 points
495 Posts
12-26-2008 3:06 AM |
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 }
Please Mark as Answer if this helps you.
12-26-2008 3:25 AM |
That Worked! Thanks a Lot Dude