471,338 Members | 1,305 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Linq questions

Yesterday I was working on a code where I need to split the parameters
and create dictionary from it. The string is something like the
following

name key1=value1 key2=value2

here is the code

string[][] pharsed = argument.Skip(1).Select(s =>
s.split(’=')).ToArray();

IDictionary<string, stringdictionary = new Dictionary<string,
string>();

foreach(string[] vaue in pharsed)

{

dictionary.add(vaue[0], vaue[1]);

}

Just want to know, is it possible to create the dictionary also in the
same line of code instead of doing it like I am trying?

http://myfavoritemovies.us/csharp/?p=21
Sep 3 '08 #1
2 1486
On Wed, 03 Sep 2008 09:05:32 -0700, CSharper <cs******@gmx.comwrote:
Yesterday I was working on a code where I need to split the parameters
and create dictionary from it. The string is something like the
following

name key1=value1 key2=value2

here is the code

string[][] pharsed = argument.Skip(1).Select(s =>
s.split(’=')).ToArray();

IDictionary<string, stringdictionary = new Dictionary<string,
string>();

foreach(string[] vaue in pharsed)

{

dictionary.add(vaue[0], vaue[1]);

}

Just want to know, is it possible to create the dictionary also in the
same line of code instead of doing it like I am trying?
I think so, sort of. Granted, I think the code you're using seems fine
as-is, but if you want to go whole-hog with the LINQ, here are a couple of
options I think would work for you:

IDictionary<string, stringdictionary = new Dictionary<string,
string>();
argument.Skip(1).Aggregate(delegate (string strIgnore, string strCur)
{
string[] rgstrKeyValue = strCur.Split('=');

dictionary.Add(rgstrKeyValue[0], rgstrKeyValue[1]);
return null;
});

Some might consider that an abuse of the Aggregate() method, but without a
ForEach(), it seems to do. Alternatively, you could do:

IDictionary<string, stringdictionary = new Dictionary<string,
string>();
argument.Skip(1).ToList().ForEach(delegate (string strCur)
{
string[] rgstrKeyValue = strCur.Split('=');

dictionary.Add(rgstrKeyValue[0], rgstrKeyValue[1]);
});

Maybe a bit more expressive, at the cost of converting the input to a
List<stringfirst.

Note that in neither case is it actually a one-liner. I suppose you could
make it briefer by doing the Split() twice, but I just can't bring myself
to do that just to make shorter code, so the above winds up having to
cache the Split() result in a local.

Pete
Sep 3 '08 #2
On Sep 3, 12:05*pm, CSharper <cshar...@gmx.comwrote:
Yesterday I was working on a code where I need to split the parameters
and create dictionary from it. The string is something like the
following

name key1=value1 key2=value2

here is the code

string[][] pharsed = argument.Skip(1).Select(s =>
s.split(’=')).ToArray();

IDictionary<string, stringdictionary = new Dictionary<string,
string>();

foreach(string[] vaue in pharsed)

{

* * *dictionary.add(vaue[0], vaue[1]);

}

Just want to know, is it possible to create the dictionary also in the
same line of code instead of doing it like I am trying?

http://myfavoritemovies.us/csharp/?p=21
Hi,

How do you separate between a value and the next keyname?
Sep 3 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by shapper | last post: by
28 posts views Thread by Marc Gravell | last post: by
1 post views Thread by Frederik | last post: by
2 posts views Thread by stephen | last post: by
9 posts views Thread by =?Utf-8?B?cmF1bGF2aQ==?= | last post: by
7 posts views Thread by shapper | last post: by
2 posts views Thread by shapper | last post: by
4 posts views Thread by CSharper | last post: by
4 posts views Thread by George | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.