Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I serialize a string like this: "Yes,No" into something like this {"0":"Yes", "1":"No"} ?
I have split the string by ',' and I've stored value into a KeyValuePair<int,string>. After serialization, the result is something like this:
{"Key" : "0" , "Value" : "Yes"},{"Key":"1","Value":"No"}
var result = yourString.Split(',').Select((v, k) => new { k, v, })
.ToDictionary(a => a.k, a => a.v);
You could always just serialize it yourself:
string theString = "True,False";
var result = theString.Split(',')
.Select((v, i) => string.Format("\"{0}\":\"{1}\"", i, v));
To get the final result you're asking for you could do this:
String.Join(",", input.Split(',')
.Select((s,i) => "{\"Key\":\""+i+"\",\"Value\":\""+s+"\"}"));
string s = "yes,no";
var d = s.Split(',');
var result = new Dictionary<int,string>(){};
for (int i = 0; i < d.Length; i++) {
result.Add(i, d[i]);
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need to do a LINQ expression where the expression varies on how deep the expected node is in the hierarchy.
So I use concat on a string like this:
var parString = string.Concat(Enumerable.Repeat(".SelectMany(f => f.level)", level));
so if its 4 levels deep I get the string :
string1 = ".SelectMany(f => f.level).SelectMany(f => f.level).SelectMany(f => f.level).SelectMany(f => f.level)"
I then want to use this string in a LINQ expression, example:
List + string1 + .FirstOrDefault(.......);
Is this even possible? How can I do it?
Use a for loop to determine how many times to call .SelectMany:
var query = List;
for(int depth = 4; depth > 0; depth--)
{
query = query.SelectMany(f => f.level);
}
// Materialize query with `FirstOrDefault` or anything you need
Notice that currently if you reach the maximum depth and still continue you will get an exception. To solve that you can add an if statement to check that f.level is not null or not empty depending on your logic. Something like:
for(int depth = 4; depth > 0; depth--)
{
query = query.SelectMany(f => f.level ?? Enumerable.Empty<YourType>());
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi there I have bunch of string array and there may or may not be null or empty in fields (see below), how do I use the simplest codes to convert them to string '0'?
The goal is to turn the following array
string[] NullFields = {"101.002","A","12","","3","","B"};
to be like
string[] NullFields = {"101.002","A","12","0","3","0","B"};
Thank you very much.
How about a simple loop?
for (int i = 0; i < NullFields.Length; i++) {
if (string.IsNullOrEmpty(NullFields[i])
NullFields[i] = "0";
}
This is easy with LINQ:
string[] NullFields = {"101.002","A","12","","3","","B"}
.Select(x => string.IsNullOrEmpty(x) ? "0" : x)
.ToArray();
First of all "" != null in C# and there's a huge difference between the two - bedtime reading.
Second of all, here you go:
var myArray = NullFields.Select(s => String.IsNullOrEmpty(s) ? "0" : s)
.ToArray();
This will solve your problem.
string[] NullFields = { "101.002", "A", "12", "", "3", "", "B" };
for (int i = 0; i < NullFields.Length; i++)
{
if (NullFields[i] == null || NullFields[i] == "")
NullFields[i] = "0";
}
Hope it will help you !
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The question is on C#.
I have a string "value1=1234;value2=2345;value3=3456;value4= ..."
What is the best way to retrieve the values?
I thought about String.Split(";") but I don't know how to retrieve the values only. The result I get includes the prefix I don't want.
I only want the values of "1234", "2345", "3456"... nothing else, and them put them into a list of strings.
How do I solve this? Thanks.
If the format is always fixed, you can do it fairly easily via LINQ:
List<string> values = theString.Split(';').Select(s => s.Split('=')[1]).ToList();
Note that you may want to use RemoveEmptyEntries if your input string ends in a semi-colon:
List<string> values = theString
.Split(new[]{';'}, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Split('=')[1]).ToList();
This would prevent an exception from occuring within the Select. If the input doesn't end in a semi-colon, however, this wouldn't be necessary.
var text = "value1=1234;value2=2345;value3=3456;value4= ...";
var pieces = text.Split('=');
var values = new Dictionary<string,string>();
for(int index = 0; index < pieces.Length; index += 2)
{
values.Add(pieces[index], pieces[index + 1]);
}
This will give you a dictionary of the pairs where the key is the left-hand side of the '=' and the value is the string representation of the value, which allows your to do:
var value1 = values["value1"];
var value2 = values["value2"];
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Improve this question
i have a following data in 3 sets.
EUR5-002,EUR10-000,
EUR20-001,EUR50-001,
EUR100-001,EUR200-000,
EUR500-000
EUR5-000,EUR10-000,
EUR20-002,EUR50-001,
EUR100-001,EUR200-000,
EUR500-000
EUR5-001,EUR10-001,
EUR20-002,EUR50-001,
EUR100-002,EUR200-003,
EUR500-000
Here EUR - CurrencyID and 5,10,20,50,100,200,500 are currency values. And the values after "-" is no of notes of the corresponding denomination.(EUR5-002, means 2 notes of 5 EUROs)
In my code I have read each set as string and added to List.
I need a logic in C# using regex or someother to add each individual denomination count from all 3 sets of data.
From the above data, I have to get the output as below.
EUR5-003,EUR10-001,
EUR20-005,EUR50-003,
EUR100-004,EUR200-003,
EUR500-000
Code does not handles any errors which can be caused by wrong input format, you can maintain it for yourself.
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var data = new [] {
"EUR5-002,EUR10-000,EUR20-001,EUR50-001,EUR100-001,EUR200-000,EUR500-000",
"EUR5-000,EUR10-000,EUR20-002,EUR50-001,EUR100-001,EUR200-000,EUR500-000",
"EUR5-001,EUR10-001,EUR20-002,EUR50-001,EUR100-002,EUR200-003,EUR500-000"
};
var results = new Dictionary<string, int>();
foreach (var line in data)
{
var entries = line.Split(',');
foreach (var entry in entries)
{
var parts = entry.Split('-');
string key = parts[0];
if (!results.ContainsKey(key))
{
results[key] = 0;
}
results[key] += int.Parse(parts[1]);
}
}
foreach (var result in results)
{
Console.WriteLine(result.Key + "-" + result.Value.ToString("000"));
}
Console.ReadLine();
}
}
}
You can do it this way
//limit is the highest value of currency
int temp=0;
for(int x=1;x<=limit;x++,temp=0)
{
if((temp=parseDenomination(x,input))!=0)
output+=","+"EUR"+x+"-"+temp;
}
//output has your desired output
private static int parseDenomination(int no,String input)
{
return Regex.Matches(input,#"(?<=EUR"+no+#"-)\d+")
.Cast<Match>()
.Select(x=>int.Parse(x.Groups[0].Value))
.ToList()
.Sum();
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to convert ILIST<string> to ILIST <object>
How can achieve this using C#?
var oList = (IList<object>)sList.Cast<object>().ToList();
IList<string> s = new List<string>() ;
IList<object> o = new List<object>();
foreach (string x in o)
s.Add(x);
The following should do it:
IList<String> strings = new List<String> { "a", "b", };
IList<Object> objects = strings.Cast<Object>().ToList();
You can test the results in LinqPad by calling: objects.Dump();
stringList.Select(x => (object)x).ToList();
that is what the Lambda expressions are for.
you can use a simple Lambda expression for the conversion. Provided you are on .NET 2.0 or above.
eg:
stringList.Select(x => (object)x).ToList();