From: Tony Johansson on 30 Apr 2010 14:15 Hi! Here I use LINQ to XML. What I want is to load the following elements title, runningLength, productionYear,url and isbnNumber into a generic collection of Movie object. By code works but I find it far to long. It must be possible to make this code much shorter. List<Movie> movies = new List<Movie>(); var myMovies = from movie in XDocument.Load(TextboxURL.Text).Descendants("movie") select new { title = movie.Element("title").Value, runningLength = movie.Element("runningLength").Value, productionYear = movie.Element("productionYear").Value, url = movie.Element("url").Value, isbnNumber = movie.Element("isbnNumber").Value }; foreach (var movie in myMovies ) { movies.Add(new Movie( movie.title, movie.url,Convert.ToInt32 (movie.runningLength),movie.productionYear,movie.isbnNumber)); } //Tony
From: Family Tree Mike on 30 Apr 2010 14:38 On 4/30/2010 2:15 PM, Tony Johansson wrote: > Hi! > > Here I use LINQ to XML. What I want is to load the following elements > title, runningLength, productionYear,url and isbnNumber into a generic > collection of Movie object. > By code works but I find it far to long. It must be possible to make this > code much shorter. > > List<Movie> movies = new List<Movie>(); > var myMovies = from movie in > XDocument.Load(TextboxURL.Text).Descendants("movie") > select new > { > title = movie.Element("title").Value, > runningLength = movie.Element("runningLength").Value, > productionYear = movie.Element("productionYear").Value, > url = movie.Element("url").Value, > isbnNumber = movie.Element("isbnNumber").Value > }; > > foreach (var movie in myMovies ) > { > movies.Add(new Movie( movie.title, movie.url,Convert.ToInt32 > (movie.runningLength),movie.productionYear,movie.isbnNumber)); > } > > //Tony > > Your code only is long because you are creating the anonymous type via LINQ, then converting the results to the type you want. Depending on your needs after creating the list, couldn't you simply do: List<Movie> movies = from movie in XDocument.Load(TextboxURL.Text).Descendants("movie") select new Movie { title = movie.Element("title").Value, runningLength = movie.Element("runningLength").Value, productionYear = movie.Element("productionYear").Value, url = movie.Element("url").Value, isbnNumber = movie.Element("isbnNumber").Value }; -- Mike
From: Harlan Messinger on 30 Apr 2010 14:37 Tony Johansson wrote: > Hi! > > Here I use LINQ to XML. What I want is to load the following elements > title, runningLength, productionYear,url and isbnNumber into a generic > collection of Movie object. > By code works but I find it far to long. It must be possible to make this > code much shorter. > > List<Movie> movies = new List<Movie>(); > var myMovies = from movie in > XDocument.Load(TextboxURL.Text).Descendants("movie") > select new > { > title = movie.Element("title").Value, > runningLength = movie.Element("runningLength").Value, > productionYear = movie.Element("productionYear").Value, > url = movie.Element("url").Value, > isbnNumber = movie.Element("isbnNumber").Value > }; > > foreach (var movie in myMovies ) > { > movies.Add(new Movie( movie.title, movie.url,Convert.ToInt32 > (movie.runningLength),movie.productionYear,movie.isbnNumber)); > } Untested, but: List<Movie> movies = ( from movie in XDocument.Load(TextboxURL.Text).Descendants("movie") select new Movie( movie.Element("title").Value, movie.Element("url").Value, Convert.ToInt32(movie.Element("runningLength").Value), movie.Element("productionYear").Value, movie.Element("isbnNumber").Value ) ).ToList();
|
Pages: 1 Prev: Trouble calculating exact values with double datatypes Next: Aggregate unions |