-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);      
Bassie
  • 9,529
  • 8
  • 68
  • 159
Gayathri
  • 17
  • 1
  • 7
  • @HimBromBeere To be honest they've actually tried something, just not bothered googling when they got their unexpected result. – TheLethalCoder May 08 '17 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? – C. McCoy IV May 08 '17 at 12:47

4 Answers4

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);
Sweeper
  • 213,210
  • 22
  • 193
  • 313
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();
orhun.begendi
  • 937
  • 3
  • 16
  • 31
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

ganeshran
  • 3,512
  • 7
  • 41
  • 69
  • It prints correctly but at end it shows some exception – Gayathri May 08 '17 at 12:57
  • Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Program. – Gayathri May 08 '17 at 12:57
  • 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 – Gayathri May 08 '17 at 12:57
  • 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 – ganeshran May 08 '17 at 13:00
  • for(i=0;i – Gayathri May 08 '17 at 13:11
  • Make Sure n is equal to the Length of the Player array. The last index of the array probably doesn't get a Player instance. You can check this by putting a breakpoint on Console.WriteLine("Player list:") and putting a watch on p[n]. See if it is null – ganeshran May 08 '17 at 13:14
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);
Gururaj
  • 539
  • 2
  • 8
  • It works but at the end got exception – Gayathri May 08 '17 at 13:12
  • What is the exception you're seeing? – Gururaj May 08 '17 at 13:18
  • 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 – Gayathri May 08 '17 at 13:22
  • Please add your program here. Let me see what's going wrong – Gururaj May 08 '17 at 13:25
  • 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 – Gayathri May 08 '17 at 13:26
  • public class Player { private string _playerName; public string PlayerName { get{return _playerName;}set{this._playerName=value;} } public Player(){} public Player(string _playerName){this._playerName=_playerName;} } – Gayathri May 08 '17 at 13:26
  • @Gayathri - The problem is on p[n]=new Player(); where you're creating an object unnecessarily and not setting the Player name. – Gururaj May 09 '17 at 09:22