2012-03-07 4 views
1

のリストとして匿名型を返す....は、私は当時のものを抱えている文字列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>を変換できません。明示的な変換 が存在します(キャストがありませんか?)

試したキャスティングは喜んでいません。私は間違って何をしていますか?

+2

あなたは、文字列をしたい場合は、文字列ではなく、匿名型を選択する必要があります。 – SLaks

+0

@SLaks:あなたはそうです...今すぐすべてのシリンダーで発砲しません。疲れて寒いです:( –

答えて

5

私はあなたがおそらくしたいと思う:

Crops = x.Crops.Select(c => c.Name).OrderBy(name => name).ToList() 
3

は試してみてください。

Crops = x.Crops.Select(crop => crop.Name) // Sequence of strings 
       .OrderBy(name => name) // Ordered sequence of strings 
       .ToList() // List of strings 
+1

2秒で殴られます。 –

関連する問題