首页 新闻 会员 周边 捐助

LINQ左联接的问题

0
[已解决问题] 解决于 2009-10-22 16:38

        我从查询表达式转成查询操作符的写法,但是结果总是不对

        //查询表达式
        GridViewLeftOuterJoin.DataSource =
          from publisher in SampleData.Publishers
          join book in SampleData.Books on publisher equals book.Publisher into publisherBooks
          from book in publisherBooks.DefaultIfEmpty ()
          select new {
              Publisher = publisher.Name ,
              Book = book == default ( Book ) ? "(no books)" : book.Title
          };
        GridViewLeftOuterJoin.DataBind ();
         正确结果:
            //Publisher      Book
            //FunBooks       Funny Stories
            //FunBooks       Bonjour mon Amour
            //Joe Publishing LINQ rules
            //Joe Publishing C# on Rails
            //Joe Publishing All your base are belong to us
            //I Publisher    (no books)

       

        //查询操作符
        GridViewLeftOuterJoin1.DataSource = SampleData.Publishers
                                            .Join ( SampleData.Books ,
                                                        p => p ,
                                                        b => b.Publisher ,
                                                        ( b , pb ) => new {
                                                            Publisher = b.Name ,
                                                            Book = pb == default ( Book ) ? "(no books)" : pb.Title
                                                        } ).DefaultIfEmpty ();

        GridViewLeftOuterJoin1.DataBind ();
         结果(错误):
            //Publisher      Book
            //FunBooks       Funny Stories
            //FunBooks       Bonjour mon Amour
            //Joe Publishing LINQ rules
            //Joe Publishing C# on Rails
            //Joe Publishing All your base are belong to us


以下为数据源:
 static public class SampleData
  {
    static public Publisher[] Publishers =
    {
      new Publisher {Name="FunBooks"},
      new Publisher {Name="Joe Publishing"},
      new Publisher {Name="I Publisher"}
    };
    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]
      }
    };
  }

chenming的主页 chenming | 初学一级 | 园豆:0
提问于:2009-10-22 15:31
< >
分享
最佳答案
0

 

 GridViewLeftOuterJoin1.DataSource =
SampleData.Publishers
         .GroupJoin ( SampleData.Books , p
=> p , b => b.Publisher ,
                         ( b , pb )
=> new { b , pb } )
         .SelectMany ( pb
=> pb.pb.DefaultIfEmpty () ,
                         ( b , pb )
=> new {
                          Publisher
= b.b.Name ,
                          Book
= pb == default ( Book )
                                     
? "(no books)" : pb.Title
          } );

        GridViewLeftOuterJoin1.DataBind ();

RicoRui | 老鸟四级 |园豆:3663 | 2009-10-22 15:59
ths
chenming | 园豆:0 (初学一级) | 2009-10-22 16:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册