locked
How to use LINQ to Join Related Data in E-Commerce Site RRS feed

  • Question

  • User-242280562 posted

    I am working on an E-commerce site where products have more than one price. How would I code a LAMBDA/LINQ expression to join the products with the appropriate prices? I thought I had it when I used the code that filtered the books by genre, but when I ran the application, every price is shown for every book! What did I do wrong?

    <asp:ListView ID="BookList" runat="server"
    		DataKeyNames="BookID" GroupItemCount="4"
    		ItemType="LethalLibrary.Book" SelectMethod="GetBooks">
    		<EmptyDataTemplate>
    			<table>
    				<tr>
    					<td>No data was returned.</td>
    				</tr>
    			</table>
    		</EmptyDataTemplate>
    		<EmptyItemTemplate>
    			<td />
    		</EmptyItemTemplate>
    		<GroupTemplate>
    			<tr id="itemPlaceholderContainer" runat="server">
    				<td id="itemPlaceholder" runat="server"></td>
    			</tr>
    		</GroupTemplate>
    		<ItemTemplate>
    			<td runat="server">
    				<table>
    					<tr>
    						<td style="vertical-align: top;">
    							<a href="<%#: GetRouteUrl("BookByTitleRoute", New With {.bookTitle = Item.BookTitle})%>">
    								<img src="/Content/Images/DB/<%#:Item.ImagePath%>" class="img-fluid img-thumbnail" /></a>
    						</td>
    					</tr>
    					<tr>
    						<td>
    							<a href="BookDetails.aspx?BookID=<%#:Item.BookID%>">
    								<h3>
    									<%#:Item.BookTitle%>
    								</h3>
    							</a>
    							<br />
    							<asp:Repeater ID="REP_Prices" runat="server" ItemType="LethalLibrary.Price" SelectMethod="GetPrices" DataMember="PriceID">
    								<HeaderTemplate>
    									<div class="btn-group-vertical btn-group-toggle" data-toggle="buttons">
    								</HeaderTemplate>
    								<FooterTemplate>
    									</div>
    								</FooterTemplate>
    								<ItemTemplate>
    									<asp:Label runat="server" CssClass="btn btn-block btn-danger">
    									<asp:RadioButton ID="RB_PriceFormat" runat="server" GroupName="PriceFormat" /><strong><%#:Item.Label %>:</strong>&nbsp;<%#Item.Value.ToString("c") %>
    									</asp:Label>
    								</ItemTemplate>
    							</asp:Repeater>
    							<br />
    						</td>
    					</tr>
    					<tr>
    						<td>&nbsp;</td>
    					</tr>
    				</table>
    				</p>
    			</td>
    		</ItemTemplate>
    		<LayoutTemplate>
    			<table style="width: 100%;">
    				<tbody>
    					<tr>
    						<td>
    							<table id="groupPlaceholderContainer" runat="server" style="width: 100%">
    								<tr id="groupPlaceholder"></tr>
    							</table>
    						</td>
    					</tr>
    					<tr>
    						<td></td>
    					</tr>
    					<tr></tr>
    				</tbody>
    			</table>
    		</LayoutTemplate>
    	</asp:ListView>
    
    Public Function GetPrices(<QueryString("BookID")> bookId As Nullable(Of Integer)) As IQueryable(Of Price)
    		Dim db = New BookContext
    		Dim query As IQueryable(Of Price) = db.Prices
    		If bookId.HasValue AndAlso bookId > 0 Then
    			query = query.Where(CType(Function(p) p.BookID = bookId, Func(Of Book, Boolean)))
    		End If
    
    		Return query
    	End Function

    Sunday, May 6, 2018 9:11 PM

All replies

  • User283571144 posted

    Hi Kelli Jo Isaak,

    According to your description and codes, I found you call SelectMethod in the Repeater control.

    But I guess you don't pass the book parameter to the Repeater control to get the price according to the book id.

    I suggest you could post the details codes about your page and code-behind.

    If you could post more details information, it will be more easily for us to find the reason and give you solution.

    Best Regards,

    Brando

    Tuesday, May 8, 2018 2:40 AM
  • User-242280562 posted

    Hi, Brando!

    I am so sorry for not answering sooner; it's been a busy week. Here is my markup and my codebehind for the page. I would appreciate any advice you have! I also appreciate seeing the new code written. Thank you so much!

    <div class="row">
    		<asp:Repeater ID="REP_BookList" runat="server" ItemType="LethalLibrary.Book" SelectMethod="GetBooks">
    			<ItemTemplate>
    				<div class="col-12 col-sm-12 col-md-3 col-lg-3 col-xl-3">
    					<div class="card">
    						<div class="card-header">
    							<a class="text-warning text-center font-weight-bold" href="BookDetails.aspx?BookID=<%#:Item.BookID%>">
    								<h3 class="card-title">
    									<%#:Item.BookTitle%>
    								</h3>
    								<h4>
    									by <%#: Item.Author.FirstName %>&nbsp;<%#: Item.Author.LastName %>
    								</h4>
    							</a>
    						</div>
    						<img src="/Content/Images/DB/<%#:Item.ImagePath%>" class="card-img-top img-thumbnail border-warning" />
    						<div class="card-body">
    							<asp:Repeater ID="REP_Prices" runat="server" ItemType="LethalLibrary.Price" SelectMethod="GetPrices" DataMember="PriceID">
    								<HeaderTemplate>
    									<div class="btn-group-vertical btn-group-toggle" data-toggle="buttons">
    								</HeaderTemplate>
    								<FooterTemplate>
    									</div>
    								</FooterTemplate>
    								<ItemTemplate>
    									<asp:Label runat="server" CssClass="btn btn-block btn-danger">
    										<asp:RadioButton ID="RB_PriceFormat" runat="server" GroupName="PriceFormat" /><strong><%#:Item.Label %>:</strong>&nbsp;<%#Item.Value.ToString("c") %>
    									</asp:Label>
    								</ItemTemplate>
    							</asp:Repeater>
    						</div>
    						<div class="card-footer">
    							<a class="btn btn-lg btn-block btn-outline-success font-weight-bold" href="/AddToCart.aspx?BookID=<%#:Item.BookID %>">Add To Cart
    							</a>
    						</div>
    					</div>
    				</div>
    			</ItemTemplate>
    		</asp:Repeater>
    	</div>
    
    Public Function GetBooks(<QueryString("BookID")> genreId As Nullable(Of Integer), <RouteData> genreName As String) As IQueryable(Of Book)
    		Dim db = New BookContext
    		Dim query As IQueryable(Of Book) = db.Books
    
    		If genreId.HasValue AndAlso genreId > 0 Then
    			query = (query.Where(CType(Function(p) p.GenreID = genreId, Func(Of Book, Boolean)))).AsQueryable()
    		End If
    
    		Return query
    	End Function
    
    	Public Function GetPrices(<QueryString("BookID")> bookId As Nullable(Of Integer)) As IQueryable(Of Price)
    		Dim db = New BookContext
    		Dim query As IQueryable(Of Price) = db.Prices
    		If bookId.HasValue AndAlso bookId > 0 Then
    			query = query.Where(CType(Function(p) p.BookID = bookId, Func(Of Book, Boolean))).AsQueryable()
    		End If
    
    		Return query
    	End Function
    
    	Public Function GetAuthors(<QueryString("BookID")> bookId As Nullable(Of Integer)) As IQueryable(Of Author)
    		Dim db = New BookContext
    		Dim query As IQueryable(Of Author) = db.Prices
    		If bookId.HasValue AndAlso bookId > 0 Then
    			query = query.Where(CType(Function(p) p.BookID = bookId, Func(Of Book, Boolean))).AsQueryable()
    		End If
    
    		Return query
    	End Function

    Monday, May 14, 2018 6:48 PM