のリストとして匿名型を返す....は、私は当時のものを抱えている文字列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
@SLaks:あなたはそうです...今すぐすべてのシリンダーで発砲しません。疲れて寒いです:( –