一般将 IQueryable 列表转换为 dto 对象列表
Generically convert IQueryable list to a list of dto objects
背景
我有一个扩展方法,可以将 IQueryable<> 列表转换为 IEnumerable<>:
public static IEnumerable<PersonDto> ToDtoList(
this IQueryable<Person> source)
{
var result = new List<PersonDto>();
foreach (var item in source)
{
result.Add(item.ToDto());
}
return result;
}
item.ToDto
扩展执行此操作:
public static PersonDto ToDto(this Person source)
{
if (source == null)
return null;
return new PersonDto
{
PersonId = source.personId,
Firstname = source.firstname,
Lastname = source.lastname,
DateOfBirth = source.dateOfBirth,
CreateDate = source.createDate,
ModifyDate = source.modifyDate,
};
}
问题
有没有办法配置以下内容以便 item.ToDto()
工作?
public static IEnumerable<T2> ToDtoList<T, T2>(this IQueryable<T> source)
{
var result = new List<T2>();
foreach (var item in source)
{
result.Add(item.ToDto());
}
return result;
}
照原样,它不起作用,因为 .ToDto
是 item
的无法解析的符号。
问题(如您所知)是如何 "generically" 将 T
映射到 T2
?
您可以使用像 AutoMapper 这样的工具,您可以将其配置为在任何两种类型之间进行一般映射,或者您可以为映射函数添加一个参数:
public static IEnumerable<T2> ToDtoList<T, T2>(this IQueryable<T> source, Func<T, T2> map)
{
var result = source.AsEnumerable() // to avoid projecting the map into the query
.Select(s => map(s));
return result;
}
背景
我有一个扩展方法,可以将 IQueryable<> 列表转换为 IEnumerable<>:
public static IEnumerable<PersonDto> ToDtoList(
this IQueryable<Person> source)
{
var result = new List<PersonDto>();
foreach (var item in source)
{
result.Add(item.ToDto());
}
return result;
}
item.ToDto
扩展执行此操作:
public static PersonDto ToDto(this Person source)
{
if (source == null)
return null;
return new PersonDto
{
PersonId = source.personId,
Firstname = source.firstname,
Lastname = source.lastname,
DateOfBirth = source.dateOfBirth,
CreateDate = source.createDate,
ModifyDate = source.modifyDate,
};
}
问题
有没有办法配置以下内容以便 item.ToDto()
工作?
public static IEnumerable<T2> ToDtoList<T, T2>(this IQueryable<T> source)
{
var result = new List<T2>();
foreach (var item in source)
{
result.Add(item.ToDto());
}
return result;
}
照原样,它不起作用,因为 .ToDto
是 item
的无法解析的符号。
问题(如您所知)是如何 "generically" 将 T
映射到 T2
?
您可以使用像 AutoMapper 这样的工具,您可以将其配置为在任何两种类型之间进行一般映射,或者您可以为映射函数添加一个参数:
public static IEnumerable<T2> ToDtoList<T, T2>(this IQueryable<T> source, Func<T, T2> map)
{
var result = source.AsEnumerable() // to avoid projecting the map into the query
.Select(s => map(s));
return result;
}