我有一天......
这是我的班级:
/// <summary>
/// Represent a trimmed down version of the farms object for
/// presenting in lists.
/// </summary>
public class PagedFarm
{
/// <summary>
/// Gets or sets Name.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets Slug.
/// </summary>
public string Slug { get; set; }
/// <summary>
/// Gets or sets Rating.
/// </summary>
public int Rating { get; set; }
/// <summary>
/// Gets or sets City.
/// </summary>
public string City { get; set; }
/// <summary>
/// Gets or sets Crops.
/// </summary>
public List<string> Crops { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我将我的父Farm实体解析为PagedFarm类的微薄尝试.
int pageNumber = page ?? 1;
// Get a list of all the farms and hostels
var farms =
this.ReadOnlySession.Any<Farm>(x => x.Deleted == false).Select(
x =>
new PagedFarm
{
Name = x.Name,
Slug = x.Slug,
Rating = x.Rating,
City = x.City.Name,
// The line below doesn't work.
Crops = x.Crops.Select(c => new { c.Name })
.OrderBy(c => c.Name)
})
.ToPagedList(pageNumber, this.PageSize);
Run Code Online (Sandbox Code Playgroud)
我的错误信息:
无法隐式转换
System.Linq.IOrderedEnumerable<AnonymousType#1>为System.Collections.Generic.List<string>.存在显式转换(您是否错过了演员?)
尝试铸造但没有快乐.我究竟做错了什么?
我想你可能想要:
Crops = x.Crops.Select(c => c.Name).OrderBy(name => name).ToList()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3036 次 |
| 最近记录: |