2

I have a list of Pieces. I want an int[] of PieceID;

I try this, but this create an anonymous type[].

PiecesTop1 = new List<Piece>();

var test = PiecesTop1.Select(x => new {x.PieceID}).ToArray();

Then I found this syntaxis and works ok.

var lstPieceID = from p in PiecesTop1
                 select p.PieceID;

int[] arrPieceID = lstPieceID.ToArray();
int i = arrPieceID[0];

I usually use first sintaxis so not familiar with second one.

  • First question: How I make first sql to return an int[].

  • Second question: How can I call each of those sql query type to difference beetween them?

  • Is there any diference on what sintaxis should I use (just in case I'm not using any database).

Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
  • To return an `int[]` instead of anonymous type array use `var test = PiecesTop1.Select(x => x.PieceID).ToArray();` – Habib Jun 30 '15 at 20:09
  • @Habib Thanks for your comment. But even when `Difference between Query Expression and Method Expression in LINQ` solve question two and three, my main question was regarding the returning select. – Juan Carlos Oropeza Jun 30 '15 at 20:13
  • I tried to answer that question in my comment, I am sure there is a duplicate for that *particular* question as well on Stackoverflow, but I can't find it right now. – Habib Jun 30 '15 at 20:14
  • @Habib Yes, and you answer work great. I'm just tought wasn't ok mark my question duplicate for the secondary questions. But dont worry issue is already solve. – Juan Carlos Oropeza Jun 30 '15 at 20:20

1 Answers1

1
var test = PiecesTop1.Select(x => x.PieceID).ToArray();
Aducci
  • 26,101
  • 8
  • 63
  • 67