1

I did console application that must iterate over two dimensional array of strings and select values that contains in user input and show these values in set by "row". Unfortunately I got error System.Collections.Generic.List '1[System.String] Here is the code of application:

static void Main(string[] args)
{
    string[,] words = new string[,] 
    { 
        { "5", "" }, 
        { "10", "kare" },  
        { "20", "kanojo" },  
        { "1", "karetachi" },  
        { "7", "korosu" },  
        { "3", "sakura" },  
        { "3", "" }  
    };
    try
    {
        var pre = Console.ReadLine();
        var r = Enumerable
            .Range(0, words.GetLength(0))
            .Where(i => words[i, 1] == pre)
            .Select(i => words[i, 1])
            .OrderBy(i => words[Int32.Parse(i), 0])
            .ToList();
        Console.Write(r);
    }
    catch (Exception ex)
    {
        TextWriter errorWriter = Console.Error;
        errorWriter.WriteLine(ex.Message);
    }
    Console.ReadLine();
}

3 Answers 3

1

Your query is incorrect: you try to match each word from the list to the entirety of the user input, which means that you would always pick a single word (assuming there's no duplicates in the 2D array). Since you are sorting the results, however, it appears that you expect there to be more than one word.

To fix this, replace your selection criteria to use Contains, like this:

var r = Enumerable
    .Range(0, words.GetLength(0))
    .Where(i => pre.Contains(words[i, 1]))
    .Select(i => new {i, w=words[i, 1]})
    .OrderBy(p => Int32.Parse(words[p.i, 0]))
    .Select(p=>p.w)
    .ToList();

To display the results in a single line you could use string.Join:

Console.WriteLine("Results: {0}", string.Join(", ", r));

Note: I assume that the exercise requires you to use a 2D array. If there is no such requirement, you could use an array of tuples or anonymous types, letting you avoid parsing of the integer:

var words = new[] { 
    new { Priority = 5,  Word = "" }
,   new { Priority = 10, Word = "kare" }
,   new { Priority = 20, Word = "kanojo" }
,   ... // and so on
};

Demo.

Sign up to request clarification or add additional context in comments.

8 Comments

Your solution seemed correct but I got error "Input string was not in correct format".
No, I need to show the list in structure similar to <ul> list.
regretfully I still have this error "Input string was not in a correct format" and I do not have idea what else I can change here.
@andrey.shedko I fixed the query and added a running demo. Note that since you've got empty strings in there, they will always be included.
I saw your demo, but unfortunately, code always return two empty ("") values.
|
1

That's not an error, that's what happens when you display the result of calling the ToString function of a List.

(i.e. your statement ran correctly, you just aren't displaying it the way you think.... see?)

Try:

Console.Write(r.Aggregate((a,b) => a + "," + b));

instead of

Console.Write(r);

5 Comments

Could you advise please how should i fix that to able show the list of the words matching to user input.
To illustrate that it's not an error change it to Console.Write(r.Count()); or Console.Write(string.Join("\r\n",r.ToArray)
Updated with suggested fix :)
@Nathan, I suppose that dasblinkenlight solution should work better as it's allow me to get the list of matching words, compare to my previous code.
@andrey.shedko indeed - he gave you the solution, I just pointed out where you needed to start looking: his is the answer to accept.
0

The following code creates a 2D List as though we had this myList[][], consisting of [0] = {0,1,2,3} and [1] = {4,5,6,7,8}

List<List<int>> a2DList = new List<List<int>>()
{
    new List<int>()
    {
        0,1,2,3
    },
    new List<int>()
    {
        4,5,6,7,8
    }
};

The LINQ code a2DList.SelectMany(s => s).ToArray().Select(s => s)) returns a copy of the 2d array flattened into 1D form. SelectMany takes each element and projects each member of each element sequentially.

You could then say


var myObj = a2DList.SelectMany(s => s).ToArray().Select(s => s));

IEnumerable myEnumerable = a2DList.SelectMany(s => s).ToArray().Select(s => s));

int [] myArray = a2DList.SelectMany(s => s).ToArray().Select(s => s)).ToArray();

List myList = a2DList.SelectMany(s => s).ToArray().Select(s => s)).ToList();

etc

This is "join"ed by the string operator for printing out to Console

Console.WriteLine(string.Join(",",a2DList.SelectMany(s => s).ToArray().Select(s => s)));

// Output will be "0,1,2,3,4,5,6,7,8"

2 Comments

Could you please add some informations to your answer? Code-Only answers are not really helpful. Explain what the code is doing
Hope that makes it clearer :) Sorry about not being able to get the code to display properly.

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.