Linq返回IEnumerable <t> </t>

时间:2009-12-15 18:55:10

标签: asp.net linq linq-to-objects

我希望返回可枚举的项目以绑定嵌套网格。顶部网格显示书名,嵌套网格显示该书的作者列表。

作者集合

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)如何编写可以返回以下查询的Enumerable方法?

(当然我的实施是错误的)

 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未填充                                  作者的名字)。

Authors     Title
---------------------------
            Funny Stories
            LINQ rules
            C# on Rails
            All your base are  belong to us 
            Bonjour mon Amour

我怀疑问题出在

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>

如何改进编码以获得正确的结果?

2 个答案:

答案 0 :(得分:1)

而不是:

where book.Authors == author

您应该执行以下操作:

where book.Authors.Contains(author)

显然==不起作用,因为你将一位作者与一组作者进行比较。您想要检查书籍作者的集合是否包含指定的作者。

答案 1 :(得分:0)

不使用Eval(“作者”),而是使用Eval(“Authors.FirstName”)。

此外,您不需要书籍和作者之间的联接。您只需从book对象中选择.Authors属性即可。 (我认为作者实际上是一个拼写错误,在这种情况下应该是“作者”?)