I need to convert one object to another…in both cases of a single instance of the object as well as corresponding Lists of those objects. I’d rather not have to perform the same mappings in 2 places. Here is what I mean:
This converts a Facebook movie to a Standard Movie object
//Converts an Facebook object to a Standard Movie object
public MovieDetails ConvertFacebookMovieToStandardMovie(FacebookMovie Movies)
{
MovieDetails objMovieDetails = new MovieDetails();
objMovieDetails.ID = 0;
objMovieDetails.Source = Movies.Source;
objMovieDetails.SourceID = Convert.ToString(Movies.ID);
objMovieDetails.Title = Movies.Name;
objMovieDetails.URL = GetInternalMovieURL(objMovieDetails.Source, objMovieDetails.SourceID);
objMovieDetails.ImageURL = Movies.Picture;
objMovieDetails.SourceURL = Movies.SourceURL;
objMovieDetails.Description = Movies.Description
return objMovieDetails;
}//Convert Facebook to standard
Now I also need to do the same things in cases where I have the same objects, just in List form, i.e.
//Converts an Facebook class to a MovieDetails class of WWN
public List<MovieDetails> ConvertFacebookMovieToStandardMovie(List<FacebookMovie> lstFacebookMovieDetails)
{
List<MovieDetails> lstMovieListDetails = lstFacebookMovieDetails.Select(Movies => new MovieDetails()
{
ID = 0
,Source = Movies.Source
,SourceID = Convert.ToString(Movies.ID)
,Title = Movies.Name
,URL = GetInternalMovieURL(Source, SourceID)
,ImageURL = Movies.Picture
,SourceURL = Movies.SourceURL
,Description = Movies.Description
}).ToList();
return lstMovieListDetails;
}//Convert Facebook to standard
I’m new to c# and linq, so not sure how I could create one method that could handle both…or at least a way to encapsulate the mappings.
I know I could create an overload method for the List scenario and loop through the items in the list and call the first object convert method…But I was hoping to use the linq route I currently have for, what I’m guessing, is better performance.
Thanks!
Chad
Unless there’s a performance gain on having separate code for these two cases I’d simply scrap the single movie variant and keep the List variant. To do the single movie variant simply do this with
using System.Linq;:If you want the single case as a separate method, you can place the above as the body of that method with “…”
= new [] {movieDetail};