Click here to Skip to main content
Sign Up to vote bad
good
See more: C#ASP.NETLINQ
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 26 Nov '12 - 16:45
Edited 26 Nov '12 - 16:49

Comments
shabari7 - 27 Nov '12 - 0:39
ANY SUGGESTION

2 solutions

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:
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()));
 
  Permalink  
Comments
shabari7 - 28 Nov '12 - 0:17
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.
shabari7 - 28 Nov '12 - 0:33
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:22
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.
shabari7 - 29 Nov '12 - 0:03
okay, I just tried for 1 r 2 iterations. thank you
shabari7 - 29 Nov '12 - 0:04
ur gmail id, please
Matt T Heffron - 29 Nov '12 - 12:41
Nope. Ask any questions on Code Project. This also allows for others to answer and/or benefit from the interchange.
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.
  Permalink  
Comments
shabari7 - 27 Nov '12 - 3:19
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 ,

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 255
1 Mahesh Bailwal 230
2 Aarti Meswania 225
3 Ron Beyer 215
4 Rohan Leuva 198
0 Sergey Alexandrovich Kryukov 8,553
1 OriginalGriff 6,899
2 CPallini 3,648
3 Rohan Leuva 2,963
4 Maciej Los 2,308


Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid