我遇到的那些日子里,一个列表....返回匿名类型为字符串LINQ
这里是我的类:
/// <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; }
}
这里是我的微薄试图解析我的父母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);
我的错误信息:
无法隐式转换类型
System.Linq.IOrderedEnumerable<AnonymousType#1>
到System.Collections.Generic.List<string>
。明确转换 存在(您是否缺少演员?)
尝试投射但没有快乐。我究竟做错了什么?
如果你想字符串,你应该选择字符串,而不是匿名类型。 – SLaks 2012-03-07 15:15:43
@SLaks:你说的没错。疲惫加上有寒冷:( – 2012-03-07 15:20:00