LINQ 生成Dictionary

 

 

 

LINQ 生成Dictionary:

 

public static Dictionary<string, string> BuildDictFromString(string src, char lineDelimiter, char

wordDelimiter)

{

 

 

 

if (string

.IsNullOrEmpty(src))

{

 

 

 

return null

;

}

 

 

 

Dictionary<string, string> dest = new Dictionary<string, string

>();

 

 

 

IEnumerable<KeyValuePair<string, string

>> query =

 

 

 

from line in

src.Split(lineDelimiter)

 

 

 

let

pair = line.Split(wordDelimiter)

 

 

 

where !string.IsNullOrEmpty(pair[0]) && !string

.IsNullOrEmpty(pair[1])

 

 

 

select new KeyValuePair<string, string

>(pair[0].Trim(), pair[1].Trim());

 

 

 

foreach (KeyValuePair<string, string> entry in

query)

{

dest.Add(entry.Key.ToLower(), entry.Value.ToLower());

}

 

 

 

return

dest;

你可能感兴趣的:(C#收藏夹)