0

I have created a class called studentRecord, and it contains several properties such as Student Number, First Name, Last Name, Courses, and Credit Hours for keeping track of individual student records. I also have created a list called List<studentRecord> lstRecords = new List<studentRecord>(); that stores the various objects (students).

I understand adding a student object through using lstRecords.Add();, but am running in to trouble with editing the objects. The user is supposed to be able to enter a student number, and then be able to access and edit the properties of that specific instance of the object. I have come up with this code:

StudentRecord editRecord = lstRecords.Find(indexRecord => 
                    indexRecord.intStudentNumber == intChosenRecord);

(By the way, intChosenRecord is a variable I declared to keep track of which index they are looking for)

I understand that StudentRecord is declaring a new object of that type, and that editRecord is my new object's name. However, I run into problems with using the .Find() method. I realize that .Find() searches through the list to find something that matches up with in input. Therefore, I assume that the intChosenRecord is what the program is searching for.

However, I have no idea what indexRecord is! This is the only time that it is used within the code, and I can change it to any name I want without errors. Could someone explain what this code does, and what indexRecord is?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kendall
  • 1,992
  • 7
  • 28
  • 46
  • it is a simple alias for the element currently handled within your LINQ-expression. It could also be called SuperMan. So all in all this simply means: find all elements (let´s call them indexRecord) that fullfill the condition. – MakePeaceGreatAgain Nov 05 '14 at 13:33
  • There is an great example on http://msdn.microsoft.com/en-us/library/x0b5b5bc(v=vs.110).aspx – Florian Nov 05 '14 at 13:34
  • possible duplicate of [What does this C# code with an "arrow" mean and how is it called?](http://stackoverflow.com/questions/4829054/what-does-this-c-sharp-code-with-an-arrow-mean-and-how-is-it-called) – dav_i Nov 05 '14 at 13:36
  • easily you can do the following: Index= lstRecords.IndexOf(editRecord); – Monah Nov 05 '14 at 13:36
  • I'm not sure if Find() is the proper method in this context; I'd use [Single()](http://stackoverflow.com/questions/2724096/linq-single-vs-first) since you should be getting only one result and you'd want there to be an error if there was more than one. – BCdotWEB Nov 05 '14 at 13:44

5 Answers5

0

"indexRecord" is a variable which correspond to each student in the list. "Find" stops and returns the current student once condition (or "predicate") at the right of "=>" is true. Therefore you can name it as you want, as long as you use the same name at left and right of "=>"

A similar loop could be :

StudentRecord editRecord = null;

foreach(var indexRecord in lstRecords)
{                   
    if(indexRecord.intStudentNumber == intChosenRecord))
    {
        editRecord = indexRecord;
        break; // Exits the loop.
    }
}

This code is not very clean, but I give it for the sake of clarity, since it does the same than yours with an "oldschool" loop, which is certainly more familiar for you.

See http://msdn.microsoft.com/fr-fr/library/bb397687.aspx for more details on this syntax. And http://msdn.microsoft.com/fr-fr/library/bb397926.aspx for other methods than "Find".

AFract
  • 8,868
  • 6
  • 48
  • 70
0

indexRecord is the argument of the lambda expression. It can have any name you want. In your case, it represent a StudentRecord (an element of your list)

you can easily change your code by :

StudentRecord editRecord = lstRecords.Find(x => x.intStudentNumber == intChosenRecord);

You can learn more about lambda expression on many site, as http://www.dotnetperls.com/lambda

Xaruth
  • 4,034
  • 3
  • 19
  • 26
0

The variable editRecord refers to the match returned by Find(), so it's not creating a new object or new instance of anything; it's referring to an existing instance.

Think of indexRecord as an identifier used to iterate over all the items in a collection, like when you say:

var numbers = new List<int>();
foreach (var n in numbers)
{
    // do something with n
}

You can replace n, or indexRecord, with any identifier you like.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
0

When you are using "=>", you are using a lambda expression.

In your case, "indexRecord" is the variable name of the input parameter for your lambda expression "indexRecord.intStudentNumber == intChosenRecord". And indexRecord correspond to a student stored in your list.

I would suggest that you get familiar with the lambda expressions, because it is a powerful and common used feature of c#.

codea
  • 1,439
  • 1
  • 17
  • 31
0

try this ..

List<int> idlist=lstRecords.select(t=>t.intStudentNumber).toList();
int index=idlist.indexof(intChosenRecord);
studentRecord record=lstRecords[index];

i always use this ...

  • This answer has been automatically flagged as low-quality because there is no explanation to the code and might be deleted. Please add an explanation. – nalply Nov 05 '14 at 14:32