Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a checlistbox like :

Text-------------Value
One-------------Numeric
Two-------------Numeric
Three-----------Numeric
A---------------Character
B---------------Character
C---------------Character
D---------------Character
#---------------SplChar
$---------------SplChar
%---------------SplChar

From these list items,those items which are selected in the list, I want to build something like:

for Case:All Items Selected:
"Numeric in ('one','Two','Three') and Character in ('A','B','C','D') and SplChar in ('#','$','%')"

for Case: Random Selection say, one, three,#,$:
"Numeric in ('one','Three') and SplChar in ('#','$')"

for Case: Single Selection say, $:
"SplChar in ('$')"

I am looking for LINQ to build this string.

any help is appreciated.
Posted
Updated 26-Nov-12 16:49pm
v2
Comments
[no name] 27-Nov-12 0:39am    
ANY SUGGESTION

Hi Shabari,

Directly you can not apply LINQ on checkboxList. First you need to traverse on selected item then you can find selected item category.
 
Share this answer
 
Comments
[no name] 27-Nov-12 3:19am    
Hii Mukhtar,
var CheckedItems = from i in chklstSample.Items.Cast<ListItem>() where i.Selected == true select i;

With this I am able to get only the selected list items, but let me know how to build the string ,
I'd use .GroupBy() on the checkedItems list, grouping on the Value .
Then I'd use a .Select() to traverse the groups, using .Aggregate() on each group, with a StringBuilder as the accumulator for .Aggregate() to construct the resulting parenthesized string of Text.
Finally, use string.Join(" and ", ...) to assemble it all in one string.
Where the ... is the result of .ToArray() applied to the result of the .Select().

I tried this example:
C#
public class TestItem
{
  public string Text;
  public string Value;
  public TestItem(string text, string value)
  {
    Text = text;
    Value = value;
  }
}

// example
List<TestItem> test = new List<TestItem>();
test.Add(new TestItem("One", "Numeric"));
test.Add(new TestItem("Two", "Numeric"));
test.Add(new TestItem("Three", "Numeric"));
test.Add(new TestItem("A", "Character"));
test.Add(new TestItem("B", "Character"));
test.Add(new TestItem("C", "Character"));
test.Add(new TestItem("D", "Character"));
test.Add(new TestItem("#", "SplChar"));
test.Add(new TestItem("$", "SplChar"));
test.Add(new TestItem("%", "SplChar"));

var pieces = test.GroupBy(item => item.Value)
                 .Select(g => g.Skip(1).Aggregate(new StringBuilder(g.Key).AppendFormat(" in ('{0}'",g.First().Text), 
                                                  (sb, item) => sb.AppendFormat(",'{0}'", item.Text),
                                                  sb => sb.Append(")").ToString()));

Console.WriteLine(string.Join(" and ", pieces.ToArray()));
 
Share this answer
 
v2
Comments
[no name] 28-Nov-12 0:17am    
Thank You, can I have your email.. Could you please provide any material to learn LINQ , I am able to understand the query to some extent , I need to hone up my skills further.
[no name] 28-Nov-12 0:33am    
I used stopwatch to calculate execution time, it took around 411 ticks for linq and 102 ticks in case of general 'for loop'. Why it is so..?It indicates linq query is not preferable for string concatenation ? does linq uses more memory?
Matt T Heffron 28-Nov-12 13:22pm    
Did you accumulate the Stopwatch time over a large number of iterations? On my system, a single iteration took 5966 ticks, 10 iterations took 622 ticks/iteration, and 10000 iterations took 31 ticks/iteration. The first time the code is executed there is a (relatively) huge cost.
[no name] 29-Nov-12 0:03am    
okay, I just tried for 1 r 2 iterations. thank you
[no name] 29-Nov-12 0:04am    
ur gmail id, please

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900