473,385 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1550
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....
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.