472,964 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,964 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 1537
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: shapper | last post by:
Hello, I have been reading a few articles about LINQ and I have a few questions: 1. What do I need to start using it in my ASP.NET 2.0 / SQL 2005 / Visual Studio 2005 web sites? 2. Is...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
1
by: Frederik | last post by:
Hi all, Am I correct when stating that LINQ replaces somewhat DataTables? I have done some reading concerning LINQ, but I'm still puzzled as to whether I should use LINQ or not. My application...
1
by: Ilyas | last post by:
Hi all I have a database that contains several tables. Each table has the name data01, data02, data03 etc The column definitions for each table all contains several standard columns and then...
2
by: stephen | last post by:
Hi, I apologize for posting Linq questions here. I wanted to try to work with Linq and found a Linq (May 2006) CTP that can be used with .NET 2005. when I run the msi, i get this message: "A...
9
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how...
7
by: shapper | last post by:
Hello, Is it possible to multiply all Prices in a List<Productby 1.1 using Linq? Product has a property named Price. Thanks, Miguel
2
by: shapper | last post by:
Hello, I have the following Linq query: List<PostsTaginsert = (from t in (from t in database.Tags join p in paper.Tags on t.Name equals p.Name select t).ToList() select new PostsTag {
4
by: CSharper | last post by:
I have following XML <root> <Person id="1"> <Name>a</Name> </Person> <Person id="2"> <Name>b</Name> </Person> </root>
4
by: George | last post by:
I am a bit conservative type and usually give some time for technology to mature before starting to try it. Today my question is Linq. To start using it or not. So here is the voting questions....
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.