【问题标题】:Linq returning IEnumerable<T>Linq 返回 IEnumerable<T>
【发布时间】:2009-12-15 18:55:10
【问题描述】:

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

作者合集

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>

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

【问题讨论】:

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


    【解决方案1】:

    代替:

    where book.Authors == author
    

    你应该这样做:

    where book.Authors.Contains(author)
    

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

    【讨论】:

      【解决方案2】:

      不要使用 Eval("Authors"),而是使用 Eval("Authors.FirstName")。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多