From: Tony Johansson on
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
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
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();