Following are four different code samples to give you an idea about union operation in LINQ to XML, LINQ to Entities and LINQ to SQL.
Please note that since type "string" is a reference type in C# so I am using it in my sample code as a replacement for full fledge entity coming from a datacontext or a webservice.
string strResult1 = string.Empty; //Two generic lists of same types ListResults:lst1 = new List () { "1", "2", "3" }; List lst2 = new List () { "4", "5", "6" }; var result1 = lst1.Union(lst2).ToList(); foreach (var item in result1) strResult1 += item + Environment.NewLine; MessageBox.Show(strResult1); //Two generic lists of different types strResult1 = string.Empty; List lst3 = new List () { "1", "2", "3" }; List lst4 = new List () { 4, 5, 6 }; var result2 = lst3.Union(from a in lst4 select a.ToString()); foreach (var item in result1) strResult1 += item + Environment.NewLine; MessageBox.Show(strResult1); strResult1 = string.Empty; XDocument doc1 = XDocument.Parse(@" "); XDocument doc2 = XDocument.Parse(@" "); //Combining the elements of two XML documents var query1 = doc1.Descendants().Union(doc2.Descendants()); foreach (XElement f in query1) strResult1 += f.Name + Environment.NewLine; MessageBox.Show(strResult1); strResult1 = string.Empty; //Merging the elements of two XML documents var query2 = doc1.Descendants().Union(doc2.Descendants()).GroupBy(x => x.Name).Select(g => g.First()); foreach (XElement f in query2) strResult1 += f.Name + Environment.NewLine; MessageBox.Show(strResult1);
Two generic lists of same type
Two generic lists of different type
Combining elements of two XML documents
Merging elements of two XML documents
No comments:
Post a Comment
Your comments are highly appreciated!