C# 如何将一个通用列表转换为另一个 publicstaticlist GetAllApps(bool初始化,字符串语言) { List objApp=新列表(); List objApps=新列表(); 如果(初始化==false) { var t=ListCopy(objApps,x=>(IndianapStore\u GetAllAppsByLanguage\u ResultCache)x);//错误 objApp=admin.getAllAppsByLanguage(language.ToList(); } 其他的 { } } 公共静态列表ListCopy(列表输入,Func转换函数) { 返回输入。选择(x=>convertFunction(x)).ToList(); }
我的班级C# 如何将一个通用列表转换为另一个 publicstaticlist GetAllApps(bool初始化,字符串语言) { List objApp=新列表(); List objApps=新列表(); 如果(初始化==false) { var t=ListCopy(objApps,x=>(IndianapStore\u GetAllAppsByLanguage\u ResultCache)x);//错误 objApp=admin.getAllAppsByLanguage(language.ToList(); } 其他的 { } } 公共静态列表ListCopy(列表输入,Func转换函数) { 返回输入。选择(x=>convertFunction(x)).ToList(); },c#,entity-framework,list,C#,Entity Framework,List,我的班级 公共类IndianapStore\u GetAllAppsByLanguage\u结果缓存 { 公共长AppId{get;set;} 公共字符串AppName{get;set;} 公共字符串AppDisplayName{get;set;} 公共字符串AppDetails{get;set;} 公共字符串AppImageURL{get;set;} 公共字节[]AppImageData{get;set;} 公共长类别ID{get;set;} 公共可为空的子类别ID{get;set;} 公共字
公共类IndianapStore\u GetAllAppsByLanguage\u结果缓存
{
公共长AppId{get;set;}
公共字符串AppName{get;set;}
公共字符串AppDisplayName{get;set;}
公共字符串AppDetails{get;set;}
公共字符串AppImageURL{get;set;}
公共字节[]AppImageData{get;set;}
公共长类别ID{get;set;}
公共可为空的子类别ID{get;set;}
公共字符串AppCreatedBy{get;set;}
public System.DateTime AppCreatedOn{get;set;}
公共字符串AppModifiedBy{get;set;}
公共可为空的AppModifiedOn{get;set;}
公共可空isDeleted{get;set;}
公共可空isPromotional{get;set;}
公共字符串GenderTarget{get;set;}
公共可为空的CountryId{get;set;}
公共可为空的StateId{get;set;}
公共可为空的AgeLimitId{get;set;}
公共可为空的应用程序{get;set;}
公共可为空的AppMaxAge{get;set;}
}
我试图将一个泛型类转换为另一个泛型类,但出现此错误
IndianapStore\u GetAllAppsByLanguage\u Result
和IndianapStore\u GetAllAppsByLanguage\u ResultCache>是不同的类型,您不能像在本语句中那样将第一种类型强制转换为另一种类型:
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; }
}
如果类型具有相同的结构,您可能应该只使用一个而不是两个类型。否则,必须将数据从第一种类型复制到另一种类型。例如:
var t = ListCopy(objApps, x => (IndianAppStore_GetAllAppsByLanguage_ResultCache)x);
这很快就会变得非常乏味,一种选择是使用类似于库的方法来自动化该过程。IndianapStore\u GetAllAppsByLanguage\u ResultCache和IndianapStore\u GetAllAppsByLanguage\u结果类是否相同?是的,它们具有相同的属性,但它们位于不同的名称空间列表。ConvertAll(TOutput)-请参阅
var t = ListCopy(objApps, x => (IndianAppStore_GetAllAppsByLanguage_ResultCache)x);
var t = ListCopy(objApps, x => new IndianAppStore_GetAllAppsByLanguage_ResultCache {
AppId = x.AppId,
AppName = x.AppName,
...
});