Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 19th, 2008, 01:04 PM
Registered User
 
Join Date: Sep 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default C# to traverse through a ChechBoxList using LINQ

Hello,

This is probably one of the simplest of questions that anyone can ask. Most of you can probably answer this in your sleep :)

I am absolutely new to ASP.NET and I am trying to get my feet wet by parcticing simple excercises using version 3.5 (both VB and C#). I thank Imar for coming up with such a simple yet effective way of teaching in his book "Beginning ASP.NET 3.5 In C# and VB". I am thoroughly enjoying it and learning at a good pace.

I am trying to accomplish a simple logic to do the following. I have created a CheckBoxList with 4 items

Apple
Orange
Banana
Peach

The code from my test.aspx looks as follows...

<asp:CheckBoxList ID="cblFruits" runat="server">
 <asp:ListItem Value="1">Apple</asp:ListItem>
        <asp:ListItem Value="2">Orange</asp:ListItem>
        <asp:ListItem Value="3">Banana</asp:ListItem>
        <asp:ListItem Value="4">Peach</asp:ListItem>
</asp:CheckBoxList>

I also have a Submit button as well as a label to display the result as follows...

<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
<br />

<asp:Label ID="lblResult" runat="server" Height="80px" Width="500px"></asp:Label>
<br />

If the user checks Apple and Banana and clicks on the submit button, I want to display something like below...

In the Check Box List you selected Apple with value 1
In the Check Box List you selected Banana with value 3

I used the following c# code in btnSubmit_Click to accomplish it and it works fine.

//Loop through the Check Box list and for each selected item, display its name with appropriate message

foreach (ListItem item in cblFruits.Items)
{
 if (item.Selected == true)
 lblResult.Text += "In the Check Box List you selected " + item.Text + " with value " + item.Value + "<br />";
}

Now I want to accomplish the same using LINQ. I was able to do it in VB with the following code...


        Dim selectionResults = From member In cblFruits.Items _
                      Where member.Selected = True _
                      Select member

        For Each item As ListItem In selectionResults
            lblResult.Text &= "In Check Box List you selected " & item.Text & _
                              " with value " & item.Value & "<br />"
        Next


How can I translate the above snippet of code (that uses LINQ to traverse through the CheckBoxList items) in C#? Your help is grately appreciated.

Thanks in advance
Helios



 
Old September 20th, 2008, 04:35 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Helios,

Good question, and an interesting idea to use LINQ to solve this problem. The following code would work:
Code:
var selectionResults = from ListItem item in cblFruits.Items
                       where item.Selected == true
                       select item;

foreach (ListItem item in selectionResults)
{
  lblResult.Text += "In Check Box List you selected " + item.Text +
           " with value " + item.Value + "<br />";
}
The trick here is that you need to specify the type of the item (ListItem) in the LINQ query.

Instead of selecting ListItem instances, you can also select the entire string with LINQ:
Code:
var selectionResults = from ListItem item in cblFruits.Items
                       where item.Selected == true
                       select "In Check Box List you selected " + item.Text + 
                           " with value " + item.Value + "<br />";

foreach (string item in selectionResults)
{
  lblResult.Text += item;
}
This isn't necessarily better (data is mixed with presentational / formatting code) but it's fun to see how easy these things can be accomplished with LINQ.

Hope this helps, and it's good to hear you're enjoying the book so much.

Cheers,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old September 20th, 2008, 11:40 AM
Registered User
 
Join Date: Sep 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

Thanks for your response. It is very much appreciated.

Quote:
quote:Originally posted by Imar
This isn't necessarily better (data is mixed with presentational / formatting code) but it's fun to see how easy these things can be accomplished with LINQ.
Exactly, Imar. My idea was to explore LINQ a little bit and get a feel of how it works.I would absolutely agree that in real world this is not a good candidate for LINQing. :)

Regards,
Helios

 
Old September 20th, 2008, 12:06 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Not sure which example you are referring to, but I was referring to my second. That is, the one that returns the already formatted example isn't a very good practice. However, your initial example makes perfect sense, and is a typical scenario for what LINQ is built for....

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old September 26th, 2008, 04:37 PM
Registered User
 
Join Date: Sep 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Imar,

Thanks for the clarification. It was my misinterpretation of LINQ. Now that I read more on the topic, I started to understand it better.

Regards,
Helios






Similar Threads
Thread Thread Starter Forum Replies Last Post
traverse the whole tree without knowing the node dipsut XSLT 3 June 17th, 2011 07:49 AM
how to traverse and than print result eruditionist XSLT 4 October 28th, 2008 01:15 PM
How to traverse Directed Cyclic Graph talhas Java Basics 1 March 20th, 2008 05:33 AM
Using LINQ for this book... Lee Dumond BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 January 9th, 2008 02:05 AM
Using a Horizontal Scrollbar 2 traverse link list conundrum C# 0 January 13th, 2004 04:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.