Click here to Skip to main content
12,063,645 members (57,633 online)
Rate this:
 
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 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160204.4 | Last Updated 17 Mar 2012
Copyright © CodeProject, 1999-2016
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