Why do you print the variable type instead of the result?

Asked

Viewed 86 times

2

See the method below:

public void TesteLambda() {
            List<String> selecoes = new List<string>();
            selecoes.Add("USA");
            selecoes.Add("Brasil");
            selecoes.Add("Itália");
            selecoes.Add("França");

            // Primeira forma de fazer um filtro usando WHERE
            var melhorSelecao =
                from string s in selecoes
                where s == "Brasil"
                select s;

            Console.WriteLine(melhorSelecao);

            // Segunda forma de fazer um filtro usando WHERE
            var melhorSelecao2 = selecoes.Cast<string>().Where(s => s == "Itália");
            var melhorSelecao3 = selecoes.Where(s => s == "França");
        }

As you can see the variables should return one of the values in the list, but what is returning is this:

{System.Linq.Enumerable.WhereListIterator<string>}

What’s wrong with it?

3 answers

4


You’re actually having a list printed up and this is the result you should actually get. If you want to print the list items, then you should use a structure that goes through the list and shows your items.

using static System.Console;
using System.Collections.Generic;
using System.Linq;

public class Program {
    public static void Main() {
        var selecoes = new List<string>() { "USA", "Brasil", "Itália", "França" };
        var melhorSelecao = from string s in selecoes
                where s == "Brasil"
                select s;
        foreach (var item in melhorSelecao) {
            WriteLine(item);
        }
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • Where I can see new ways to instantiate objects or assign values to them as you did: var selectoes = new List<string>() { "USA", "Brazil", "Italy", "France" }; ?

  • It has no new forms, it has this. https://answall.com/q/126348/101

2

From what I’ve seen, you’re wearing Where, which always comes back a collection of objects. As you are returning only one object, do it this way that is best:

public void TesteLambda() {
    List<String> selecoes = new List<string>();
    selecoes.Add("USA");
    selecoes.Add("Brasil");
    selecoes.Add("Itália");
    selecoes.Add("França");

    // Primeira forma de fazer um filtro usando WHERE
    var melhorSelecao = selecoes.FirstOrDefault(a => a == "Brasil")                

    Console.WriteLine(melhorSelecao);
}

There’s no need to do it the way it was, unless you need to return a collection. If it is a single value, Firstordefault() is better.

  • It was learning level. Bigwon said what was wrong.

  • Yes, but only an additional comment so you don’t need to cast in the list element, like in "from string s in selections" or "selections. Cast<string>()". In C# String and string are the same type, so you can only use direct ;)

0

The problem is that you defined List as String a class and started as New List(); being a type of variable.

List<String> selecoes = new List<String>();

//ou

List<string> selecoes = new List<string>();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.