0
public static List<IndianAppStore_GetAllAppsByLanguage_ResultCache> GetAllApps(bool initialized, string language)
{
    List<IndianAppStore_GetAllAppsByLanguage_ResultCache> objApp = new List<IndianAppStore_GetAllAppsByLanguage_ResultCache>();

    List<IndianAppStore_GetAllAppsByLanguage_Result> objApps = new List<IndianAppStore_GetAllAppsByLanguage_Result>();

    if (initialized == false)
    {
        var t = ListCopy(objApps, x => (IndianAppStore_GetAllAppsByLanguage_ResultCache)x); // Error
        objApp = admin.getAllAppsByLanguage(language).ToList();
    }
    else
    {
    }
}

public static List<TResult> ListCopy<TSource, TResult>(List<TSource> input, Func<TSource, TResult> convertFunction)
{
    return input.Select(x => convertFunction(x)).ToList();
}

我的课

public class IndianAppStore_GetAllAppsByLanguage_ResultCache
{
    public long AppId { get; set; }
    public string AppName { get; set; }
    public string AppDisplayName { get; set; }
    public string AppDetails { get; set; }
    public string AppImageURL { get; set; }
    public byte[] AppImageData { get; set; }
    public long CategoryId { get; set; }
    public Nullable<long> SubCategoryId { get; set; }
    public string AppCreatedBy { get; set; }
    public System.DateTime AppCreatedOn { get; set; }
    public string AppModifiedBy { get; set; }
    public Nullable<System.DateTime> AppModifiedOn { get; set; }
    public Nullable<bool> isDeleted { get; set; }
    public Nullable<bool> isPromotional { get; set; }
    public string GenderTarget { get; set; }
    public Nullable<long> CountryId { get; set; }
    public Nullable<long> StateId { get; set; }
    public Nullable<long> AgeLimitId { get; set; }
    public Nullable<int> AppMinAge { get; set; }
    public Nullable<int> AppMaxAge { get; set; }
}

在此处输入图像描述

我正在尝试将一个泛型类转换为另一个泛型类,但出现此错误

4

1 回答 1

1

IndianAppStore_GetAllAppsByLanguage_Result并且IndianAppStore_GetAllAppsByLanguage_ResultCache是不同的类型,您不能像在此语句中所做的那样将第一种类型转换为另一种类型:

var t = ListCopy(objApps, x => (IndianAppStore_GetAllAppsByLanguage_ResultCache)x);

如果类型具有相同的结构,您可能应该只有一种而不是两种类型。否则,您将不得不将数据从第一种类型复制到另一种类型。例如:

var t = ListCopy(objApps, x => new IndianAppStore_GetAllAppsByLanguage_ResultCache {
  AppId = x.AppId,
  AppName = x.AppName,
  ...
});

这很快就会变得乏味,一个选择是使用像AutoMapper这样的库来自动化这个过程。

于 2013-10-14T06:32:06.307 回答