Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET LINQ C#3.5 collections , +
Hi

I have a string array, from which I want part of strings to bind dropdownlist
 

Ex:
string[] strArray = new string[10];
          
strArray[0] = "C:\MyFolder\Software";
strArray[1] = "C:\MyFolder\Admin";
strArray[2] = "C:\MyFolder\Accounts";
strArray[3] = "C:\MyFolder\HR";
strArray[4] = "C:\MyFolder\Maintainece";
 
From this string array i need "Software,Admin,Accounts,HR,Maintainece"
these strings to bind in dropdownlist without using foreach or for loop.

I need this using linq technique.. Pls help me
Posted 14-Mar-12 19:13pm
Edited 14-Mar-12 19:29pm
v2
Comments
ProEnggSoft at 15-Mar-12 0:30am
   
Edit: pre tag for C# code added - PES
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

for getting splited string you can use Split method of string datatype.
for more information on Split method can see this.
Split Method[^]
 
you can go like this.
string[] strArray = new string[5];
strArray[0] = @"C:\MyFolder\Software";
strArray[1] = @"C:\MyFolder\Admin";
strArray[2] = @"C:\MyFolder\Accounts";
strArray[3] = @"C:\MyFolder\HR";
strArray[4] = @"C:\MyFolder\Maintainece";
 
foreach (string str in strArray)
{
    string[] words =str.Split('\\');
    Response.Write(words[2] + ",<br>");
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The following LINQ expression
 
IEnumerable<string> ItemList = strArray
    .Where (a => a != null && a.Contains('\\'))
    .Select (a => a.Substring(a.LastIndexOf('\\')+1));
 
gives a collection strings, which can be bound to DropDownList
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

string[] strArray = new string[5];
strArray[0] = @"C:\MyFolder\Software";
strArray[1] = @"C:\MyFolder\Admin";
strArray[2] = @"C:\MyFolder\Accounts";
strArray[3] = @"C:\MyFolder\HR";
strArray[4] = @"C:\MyFolder\Maintainece";
 
foreach (string node in strArray)
{
    string[] strpart =node.Split('\\');
    DropdownList1.Items.Add(strpart[strpart.Length-1]);
}
 
Get last string By this code
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 17 Mar 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