I wish to return enumerable items in order to bind the nested grid. Top grid displays Book Title and the nested grid displays the authors list of that book.
Author Collection
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"}
}
Book Collection
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) How to write a Enumerable method that can return the following query ?
(ofcourse my im开发者_JAVA技巧plementation is wrong )
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) The output I received ( the nested BulletedList is not filled with author's first name).
Authors Title
---------------------------
Funny Stories
LINQ rules
C# on Rails
All your base are belong to us
Bonjour mon Amour
I suspect the problem is in
where book.Authors == author (checking Types with == operator).
Html Code
<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>
How to improve the coding to get the proper result?
Instead of:
where book.Authors == author
You should do something like:
where book.Authors.Contains(author)
Obviously == doesn't work because you compare one author to a collection of authors. You want to check if the collection of authors of the book contains the specified author instead.
Instead of using Eval("Authors"), use Eval("Authors.FirstName").
Also, you don't need the join between books and Authors. You can just select the .Authors property from the book object. (I take it that Authors is actually a typo and it should be "Author" in this case?)
精彩评论