0

I have a List in my code.

        var config = configRepository
            .ODataQueryable()
            .ToList();

        // looking for a way to send the data in variable config to the Console here

        Console.ReadLine();

I would like to view this list on the Console of my C# application. Is there some way that I can have this dumped to the console without having to manually code in a loop?

Update:

I tried a few of the methods here and they give me for the four rows:

Entities.Models.Core.Config
Entities.Models.Core.Config
Entities.Models.Core.Config
Entities.Models.Core.Config

Does anyone have any suggestion updates. It seems like the list is a list of objects. Thanks

  • http://stackoverflow.com/questions/2699466/linqpad-dump-extension-method-i-want-one – Habib Apr 09 '14 at 13:28
  • As stated, your Config object needs to override `ToString`. Make your own `ToString` to return what is relevant to each Config. – MPelletier Apr 09 '14 at 13:43

6 Answers6

2

You should be able to simply (via method-grouping) use:

config.ForEach(Console.WriteLine);

That also assumes that the objects in your list override ToString appropriately so that they display something meaningful.

EDIT:

The reason you are getting this:

Entities.Models.Core.Config
Entities.Models.Core.Config
Entities.Models.Core.Config
Entities.Models.Core.Config

..is because by default, ToString prints the full name of the object. Your Config class needs to override the ToString method. Somewhat like this:

public class Config {
    public string SomeSetting { get; set; }

    public override string ToString() {
        return string.Format("SomeSetting = {0}", SomeSetting);
    }
}
Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
1

In LINQ:

config.ForEach(c => Console.WriteLine(c.ToString());
MPelletier
  • 16,256
  • 15
  • 86
  • 137
  • Maybe ForEach not Select – csharpwinphonexaml Apr 09 '14 at 13:27
  • 1
    I'd go with a `foreach` loop if I wanted to do it like this; [best to avoid `ForEach`](http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx) (according to some opinions) – Tim S. Apr 09 '14 at 13:36
  • This give me:Entities.Models.Core.Config repeated four times. Same with a few other answers I tried. –  Apr 09 '14 at 13:39
  • 1
    @Melina You need to have your `Config` object override `ToString` so that they display meaningful information (as I mentioned in my answer). – Simon Whitehead Apr 09 '14 at 13:40
  • @TimS. Lippert is mostly warning against having it everywhere in all IEnumerables, which makes sense if you want to avoid surprises and side effects. Using it for a list that's already populated is different. – MPelletier Apr 09 '14 at 13:40
  • @MPelletier In what possible way is it different? Every single point that Eric raises in his article applies equally to `List`, none of them are specific to `IEnumerable`. – Servy Apr 09 '14 at 13:48
  • @Servy A `List` is already enumerated. Its state is finite. An `IEnumerable` might or might not have been enumerated before. Doing ForEach on an IEnumerable causes nothing but side effects. That's Lippert's point. – MPelletier Apr 09 '14 at 13:54
  • @MPelletier Yes, I'm aware of all of those facts. None of them serve as a justification for using a `ForEach` method instead of a `foreach` loop. You have provided random facts that in no way lead to the conclusion that you are attempting to demonstrate. – Servy Apr 09 '14 at 13:56
  • @Servy The facts are not random. Try to understand: what happens when you run a `foreach` on an `IEnumerable`? What goes on at every iteration? – MPelletier Apr 09 '14 at 14:00
  • @MPelletier The exact same thing that happens in a `ForEach` method, given that the `ForEach` method is nothing more than a `foreach` loop, behind the scenes. – Servy Apr 09 '14 at 14:01
  • @Servy Exactly. But having provided `ForEach` *masks* that "behind the scenes" part. Maybe you'd realize it every time you'd use `IEnumerable.ForEach`, but others might not. That's Lippert's cautionary tale: be aware of what you cause. – MPelletier Apr 09 '14 at 14:04
  • @MPelletier Yes, if you see a `ForEach` you would *hope* that it's implemented as just a `foreach`, but you can't be sure. If you see a `foreach`, you *know* what it's doing. Everyone knows what a `foreach` does, everyone needs to guess at what `ForEach` does. It provides no advantages, whatsoever, and a handful of disadvantages. It degrades the code quality, at least slightly, and improves nothing. This is the case for both `IEnumerable` and `List`, they are in no way different. – Servy Apr 09 '14 at 14:06
  • @Servy An `IEnumerable` tied to the instantiation of items at every call and a `List` of finalized objects are two very different things. `ForEach` on the first would call constructors every time it is enumerated. `ForEach` would become a facility for aiding bad implementation. – MPelletier Apr 09 '14 at 14:12
  • @MPelletier It's exactly the same as using a `foreach`. It's *not* bad to iterate a sequence and act on the items, it's simply important to recognize what it's doing. While it certainly can be done inappropriately, the same thing can be said of using a `foreach` loop. You are completely misunderstanding the problem with its use. – Servy Apr 09 '14 at 14:15
  • @Servy If you want a `ForEach` with `IEnumerable`, just make your own and live with it. Or you can ask the .NET Framework development team to implement it too, but I'm sure you'll get an answer similar to Lippert's. Perhaps if it is reworded enough times you will understand it. – MPelletier Apr 09 '14 at 14:24
  • @MPelletier I'm not saying you should use `ForEach`. I'm saying you should not. I'm saying that the reason *you* claim that one should not is not a valid reason, and the *actual* reasons for not using it are different; Eric explains them. Those reasons for not using it generally apply to `List`, just as much as they do to `IEnumerable`. *The reasons for not using `ForEach` are not specific to `IEnumerable`.* *Your* reasons for using it are specific to `IEnumerable`, but they also aren't valid to begin with. – Servy Apr 09 '14 at 14:27
  • @Servy I will make one last attempt at making you understand: every operation with `IEnumerable` is a `foreach`. That's all `IEnumerable` does. `ForEach` would only mean iterating for iteration's sake. It adds nothing, it provides no new functionality, only potential pitfalls. – MPelletier Apr 09 '14 at 14:30
  • @MPelletie I agree with that last statement. It applies equally to `List`. `ForEach` adds nothing to `List` that isn't given by `foreach`, except for some additional pitfalls. – Servy Apr 09 '14 at 14:32
1
Console.Write(string.Join("\n",config.Select(elem=>elem.ToString())));
csharpwinphonexaml
  • 3,659
  • 10
  • 32
  • 63
0

This might help you:

list.ForEach(i => Console.Write("{0}\t", i));

Taken from

Console.WriteLine and generic List

Community
  • 1
  • 1
Zshan
  • 26
  • 5
0

ObjectDumper Class is available with VS Samples :

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Samples\1033\CSharpSamples\linqSamples.

Then use :

ObjectDumper.Write(MyList);
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
0

A very simple method to print the properties of a generic object:

private string PrintObject<T>(T o)
{
    return o.GetType().GetProperties().Aggregate("", (current, p) => current + string.Format("{0}:\t{1}\n", p.Name, o.GetType().GetProperty(p.Name).GetValue(o)));
}

Try

foreach(var c in config) { PrintObject(c); }
Crypth
  • 1,576
  • 18
  • 32