2009-12-15 105 views
2

我希望返回可枚舉項目以綁定嵌套網格。頂部網格顯示書名,嵌套網格顯示該書的作者列表。Linq返回IEnumerable <T>

作者收集

static public Author[] Authors = 
{ 
    new Author {FirstName="Johnny", LastName="Good"}, 
    new Author {FirstName="Graziella", LastName="Simplegame"}, 
    new Author {FirstName="Octavio", LastName="Prince"}, 
    new Author {FirstName="Jeremy", LastName="Legrand"} 
} 

藏書

static public Book[] Books = 
{ 
    new Book 
    { 
     Title="Funny Stories", 
     Publisher=Publishers[0], 
     Authors=new[]{Authors[0], Authors[1]}, 
     PageCount=101, 
     Price=25.55M, 
     PublicationDate=new DateTime(2004, 11, 10), 
     Isbn="0-000-77777-2", 
     Subject=Subjects[0] 
    }, 
    new Book 
    { 
     Title="LINQ rules", 
     Publisher=Publishers[1], 
     Authors=new[]{Authors[2]}, 
     PageCount=300, 
     Price=12M, 
     PublicationDate=new DateTime(2007, 9, 2), 
     Isbn="0-111-77777-2", 
     Subject=Subjects[0] 
    }, 

    new Book 
    { 
     Title="C# on Rails", 
     Publisher=Publishers[1], 
     Authors=new[]{Authors[2]}, 
     PageCount=256, 
     Price=35.5M, 
     PublicationDate=new DateTime(2007, 4, 1), 
     Isbn="0-222-77777-2", 
     Subject=Subjects[0] 
    }, 
    new Book 
    { 
     Title="All your base are belong to us", 
     Publisher=Publishers[1], 
     Authors=new[]{Authors[3]}, 
     PageCount=1205, 
     Price=35.5M, 
     PublicationDate=new DateTime(2006, 5, 5), 
     Isbn="0-333-77777-2", 
     Subject=Subjects[2] 
    }, 
    new Book 
    { 
     Title="Bonjour mon Amour", 
     Publisher=Publishers[0], 
     Authors=new[]{Authors[1], Authors[0]}, 
     PageCount=50, 
     Price=29M, 
     PublicationDate=new DateTime(1973, 2, 18), 
     Isbn="2-444-77777-2", 
     Subject=Subjects[1] 
    } 
}; 

1)如何編寫可以返回以下查詢可枚舉的方法?

(ofcourse我的實現是錯誤的)

public IEnumerable<Book> GetBook() 
{ 
    IEnumerable<Book> booklist 
    = from book in SampleData.Books 
      select new Book 
      { 
      Title = book.Title, 
      Authors = 
         from author in SampleData.Authors 
         where book.Authors == author 
         select new Author 
         { 
         FirstName = author.FirstName 
         } 
      }; 
    return booklist; 
} 

2)我收到輸出(嵌套的BulletedList未充滿 作者的名字)。

我懷疑問題出在

where book.Authors == author (checking Types with == operator). 

HTML代碼

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"> 
    <Columns> 
      <asp:TemplateField HeaderText="Author List"> 
       <ItemTemplate> 
         <asp:BulletedList ID="BulletedList1" runat="server" 
         DataSource='<%# Eval("Authors") %>'> 
         </asp:BulletedList> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:BoundField DataField="Title" HeaderText="Title" 
      SortExpression="Title" /> 
     </Columns> 
</asp:GridView> 

如何提高編碼,以獲得正確的結果?

回答

1

相反的:

where book.Authors == author 

你應該這樣做:

where book.Authors.Contains(author) 

顯然==不起作用,因爲您將一位作者與作者集進行比較。您想檢查本書的作者集合是否包含指定的作者。

0

而不是使用Eval(「Authors」),使用Eval(「Authors.FirstName」)。

此外,您不需要書籍和作者之間的聯接。您可以從書對象中選擇.Authors屬性。 (我認爲作者實際上是一個錯字,它應該是「作者」在這種情況下?)