0

Before that I try to ask, i searched through the net but could not find what I am looking for.

I am trying to convert the field_values into string in C#.

type of fields_value :

public List<List<string>> field_values { get; set; }

this is my code that I want to modify it to do my work.

string x = pi.field_values[0];

I tried to do it with code below that does not work.

string x=convert.tostring(pi.field_values[0]);

any idea?

4
  • 2
    Please provide a sample of data and the desired result. At least I don't understand what you are trying to achieve exactly. Commented Feb 7, 2014 at 11:13
  • And convert how? You have a list of lists of strings. So with [0] you access the first list of strings, not a single string. Commented Feb 7, 2014 at 11:13
  • pi.field_values[0][0].ToString() Commented Feb 7, 2014 at 11:13
  • in following json format,I want to retrieve the id and then compare it with an string { "context": "65071", "names": [ "id", "name", "hash", "score", "rank" ], "values": [ [ "187126", "187126", "187126", "0.1", "92.7157" ], [ "494579", "494579", "494579", "0.05", "77.6358" ], [ "455577", "455577", "455577", "0.0488174", "76.4856" ]... Commented Feb 7, 2014 at 11:23

3 Answers 3

5

field_values[0] is a List<string> not a string.

List<string> firstFieldValues = p.field_values[0];

You could for example use String.Join to concat multiple strings:

string allValues = string.Join(",", firstFieldValues);
Sign up to request clarification or add additional context in comments.

Comments

0

field_values[0] is a List<string> ... so you have to go through those lists and save them in a string:

string allStrings = "";
foreach (var li in field_values){
    foreach (var str in li){
        allStrings += str;
    }
}

Then you'll have all fields in one long string.

Comments

0

To get strings from the first list:

var str = field_values.ElementAt(0).Aggregate((aggr, next) => aggr + ", " + next);

or from all lists:

var allStr = field_values.SelectMany(l => l).Aggregate((aggr, next) => aggr + ", " + next);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.