Join multiple table and bind into a single Listview using C# .net

.net c# dynamic-linq linq xaml

Question

I got three tables to join named Books, Borrowers and Transactions. The database scheme is as below:

  1. Books(BookID,BookName,ISBN);
  2. Borrowers(BorrowerID,BorrowerName,BorrowerLevel);
  3. Transactions(TransactionID,BorrowerID,BookID,BorrowDate,ReturnDate);

The corresponding class are Book, Borrower and Transaction respectively. Now i would like to select BookID,BookName,BorrowerID,BorrowerName,BorrowDate and ReturnDate from these three tables and to show in a listview having gridview control. XAML Code :

<Grid>
        <ListView Margin="15,57,58,57" Name="borrowedBookList" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding }" KeyDown="borrowedBookList_KeyDown">
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn Width="80" Header="Borrower ID" DisplayMemberBinding="{Binding Path=BorrowerID}"/>
                        <GridViewColumn Width="220" Header="Borrower Name" DisplayMemberBinding="{Binding Path=BorrowerName}"/>
                        <GridViewColumn Width="220" Header="Book Name" DisplayMemberBinding="{Binding Path=BookName}"/>
                        <GridViewColumn Width="100" Header="Date" DisplayMemberBinding="{Binding Path=BorrowDate}"/>
                        <GridViewColumn Width="100" Header="Return Date" DisplayMemberBinding="{Binding Path=ReturnDate}"/>
                       </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>

Now how can i join these tables and assign the result datacontext into borrowedBookList?

Thankyou

Arefin Sami

1
0
9/17/2012 6:06:14 PM

Accepted Answer

borrowedBookList.DataSource =
    from borrower in Borrowers
    join transaction in Transactions
        on borrower.BorrowerID equals transaction.BorrowerID
    join book in Books
        on transaction.BookID equals book.BookID
    select new
    {
        borrower.BorrowerID,
        borrower.BorrowerName,
        book.BookName,
        transaction.BorrowDate,
        transaction.ReturnDate,
    }

borrowedBookList.DataBind();
1
9/17/2012 6:40:34 PM

Popular Answer

have you looked into the hierarchicaldatatemplate class?

Your model or viewmodel would need to contain the respective relationships. From there, the appropriate datatemplate should work nicely.



Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow