我需要将一个对象转换为另一个对象...在对象的单个实例以及这些对象的相应列表的两种情况下。我宁愿不必在两个地方执行相同的映射。这就是我的意思: 这会将Facebook电影转换为标准电影对象
//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
现在我还需要在我拥有相同对象的情况下做同样的事情,只需要以List形式,即
//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
我是c#和linq的新手,所以不确定如何创建一个可以处理这两种方法的方法......或者至少是一种封装映射的方法。
我知道我可以为List场景创建一个重载方法并循环遍历列表中的项目并调用第一个对象转换方法......但我希望使用我目前拥有的linq路径,我是什么猜测,是更好的表现。
谢谢! 乍得
答案 0 :(得分:2)
只需打电话给你的功能。
public List<MovieDetails> ConvertFacebookMovieToStandardMovie(List<FacebookMovie> lstFacebookMovieDetails)
{
List<MovieDetails> lstMovieListDetails =
lstFacebookMovieDetails.Select(Movies => ConvertFacebookMovieToStandardMovie(Movies)).ToList();
return lstMovieListDetails;
}
或其他方式
public MovieDetails ConvertFacebookMovieToStandardMovie(FacebookMovie Movies)
{
return ConvertFacebookMovieToStandardMovie(new [] { Movies} ).FirstOrDefault();
}//Convert Facebook to standard
你也可以重载as
然后
public static MovieDetails operator as(FacebookMovie m)
{
if (m == null) return null;
MovieDetails objMovieDetails = new MovieDetails()
{
ID = 0,
Source = Movies.Source,
SourceID = Convert.ToString(Movies.ID),
Title = Movies.Name,
URL = GetInternalMovieURL(objMovieDetails.Source, objMovieDetails.SourceID),
ImageURL = Movies.Picture,
SourceURL = Movies.SourceURL,
Description = Movies.Description
}
return objMovieDetails
}
然后ConvertFacebookMovieToStandardMovie看起来很傻但看起来像这样:
public MovieDetails ConvertFacebookMovieToStandardMovie(FacebookMovie Movies)
{
return Movies as MovieDetails;
}//Convert Facebook to standard
和
public List<MovieDetails> ConvertFacebookMovieToStandardMovie(List<FacebookMovie> lstFacebookMovieDetails)
{
List<MovieDetails> lstMovieListDetails =
lstFacebookMovieDetails.Select(Movies => Movies as MovieDetails).ToList();
return lstMovieListDetails;
}
答案 1 :(得分:1)
除非为这两种情况分别设置代码,否则我只会废弃单个电影变体并保留List变体。要执行单个电影变体,只需使用using System.Linq;
执行此操作:
List<FacebookMovie> moviesDetailsList = ...
MovieDetails movieDetails = ConvertFacebookMovieToStandardMovie(moviesDetailsList).Single();
如果您希望将单个案例作为单独的方法,则可以使用“...”= new [] {movieDetail};
答案 2 :(得分:0)
较短的版本
public List<MovieDetails> ConvertFacebookMovieToStandardMovie(List<FacebookMovie> lstFacebookMovieDetails)
{
return lstFacebookMovieDetails.ConvertAll(ConvertFacebookMovieToStandardMovie);
}
根据msdn
将当前List中的元素转换为另一种类型,并返回包含已转换元素的列表。