Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET LINQ
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:45pm
Edited 26-Nov-12 16:49pm
v2
Comments
shabari7 at 27-Nov-12 0:39am
   
ANY SUGGESTION
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 at 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 ,
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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  
v2
Comments
shabari7 at 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.
shabari7 at 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 at 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.
shabari7 at 29-Nov-12 0:03am
   
okay, I just tried for 1 r 2 iterations. thank you
shabari7 at 29-Nov-12 0:04am
   
ur gmail id, please
Matt T Heffron at 29-Nov-12 12:41pm
   
Nope.
Ask any questions on Code Project.
This also allows for others to answer and/or benefit from the interchange.

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

  Print Answers RSS
0 Maciej Los 200
1 Kornfeld Eliyahu Peter 175
2 BillWoodruff 160
3 George Jonsson 140
4 OriginalGriff 136


Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100