-2

Read the player details from the user and assign it to the Player class object. Add the object to a list of type Player and write a linq query to retrieve the player names from the list. Use basic select query in LINQ.

Player[] p=new Player[100];

for(i=0;i<;i++) {
    p[i]=new Player();
    p[i].PlayerName=Console.ReadLine();
}

Console.WriteLine("Player list:");

var pl=from t in p select t;

// While printing I am getting System.Linq.Enumerable+c__Iterator10`2[Player,Player]
Console.WriteLine(pl);      
2
  • @HimBromBeere To be honest they've actually tried something, just not bothered googling when they got their unexpected result. Commented May 8, 2017 at 12:47
  • 1
    Think about it... p1 an IEnumerable contraining 100 player names. What do you expect the toString value of that to be? Commented May 8, 2017 at 12:47

4 Answers 4

0

The message was printed because the object returned by a LINQ query does not have a custom ToString method. Therefore, calling Console.WriteLine will simply print the type name.

You can print it by doing:

p.ToList().ForEach(Console.WriteLine);
Sign up to request clarification or add additional context in comments.

3 Comments

The OP is obviously a beginner, even though this is a dupe or off topic, it might be worth explaining your answer so it is clear exactly what is going on.
@TheLethalCoder Didn't I already provided an explanation in the first paragraph?
On why their code was wrong, not with how yours works.
0
var pl=from t in p select t;

Just returning the query. For get the list you should be use like this.

 var pl = (from t in p select t).ToList();

2 Comments

Then How Will I print this in line by line format?
try this, for (int i = 0; i < pl.Count; i++) { Console.WriteLine(pl[i]); }
0
 Player[] p=new Player[100];

   for(int i=0;i<p.Length;i++) {
        p[i]=new Player();
        p[i].PlayerName="SomeName";
    }

    Console.WriteLine("Player list:");

    var pl=from t in p select t.PlayerName;

    foreach(var name in pl)
        Console.WriteLine(name);

Instead of select t (which select the Player Object), select t.PlayerName. this will return an IEnumerable of PlayerName property. You can later loop through this IEnumerable and display the string

6 Comments

It prints correctly but at end it shows some exception
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Program.
m__0 (.Player t) [0x00000] in :0 at System.Linq.Enumerable+c__Iterator10`2[Player,System.String].MoveNext () [0x00000] in :0 at Program.Main (System.String[] args) [0x00000] in :0
The null reference must be coming from the code while populating the array. Make sure you are creating Player objects for every index in the array. Check the upper bound in for loop
for(i=0;i<n;i++) { p[i]=new Player(); p[i].PlayerName=Console.ReadLine(); } I am creating player objetcs for every index
|
0

The result of a LINQ is always an expression which will be executed at the time you use it a look or call a method like Distinct or so. In your case too you're seeing an Iterator for the same reason.

Change your code a little bit

var pl=from t in p select t.Name;

// While printing I am getting 
System.Linq.Enumerable+c__Iterator10`2[Player,Player]
foreach(var p in p1)
    Console.WriteLine(p);

Edit:

Your main has been revised to fix the problem

int n, i;
Console.WriteLine("Enter number of players");
n = int.Parse(Console.ReadLine());

Player[] p = new Player[n];

Console.WriteLine("Enter the player names");

for (i = 0; i < n; i++)
{
    p[i] = new Player();
    p[i].PlayerName = Console.ReadLine();
}

Console.WriteLine("Player list:");

var pl = from t in p select t.PlayerName;
foreach (var name in pl) Console.WriteLine(name);

7 Comments

It works but at the end got exception
What is the exception you're seeing?
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Program. m__0 (.Player t) [0x00000] in :0 at System.Linq.Enumerable+c__Iterator10`2[Player,System.String].MoveNext () [0x00000] in :0 at Program.Main (System.String[] args) [0x00000] in :0
Please add your program here. Let me see what's going wrong
using System; using System.Linq; using System.Collections.Generic; public class Program { public static void Main(String[] args) { int n,i; Player[] p=new Player[100]; Console.WriteLine("Enter number of players"); n=int.Parse(Console.ReadLine()); Console.WriteLine("Enter the player names"); p[n]=new Player(); for(i=0;i<n;i++) { p[i]=new Player(); p[i].PlayerName=Console.ReadLine(); } Console.WriteLine("Player list:"); var pl=from t in p select t.PlayerName; foreach(var name in pl) Console.WriteLine(name); } }
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.