我从查询表达式转成查询操作符的写法,但是结果总是不对 //查询表达式 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] } }; }
1 回答
慕莱坞森
TA贡献1810条经验 获得超4个赞
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 ();
- 1 回答
- 0 关注
- 308 浏览
添加回答
举报
0/150
提交
取消