Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please define the difference between

Student stud = context.Students.First(i => i.Roll_No == 1);
and
Student stud = context.Students.select();

and how can we use select????
Posted

1 solution

I believe you mean the Select(Func) vs First(Func) signature, as there is no parameter free signature of Select.
Well, first of all, First iterates the IEnumerable and stops as it's condition is satisfied. This means that, no matter the size of the collection, on the first occurrence that "Func" returns true, it'll stop and return the item that was being iterated.
Select(Func), on the other hand, returns another collection (IEnumerable) wich may or may not be the same underlying type as the original one. That's because Select works as a "conversor" (projects an item into another item). This also means it'll iterate through all the collection, and it'll return a second and unreferenced IEnumerable. That's really useful when you need to convert a collection or when u need to project a property of all the items of a collection into another one, for example:

var lista = new List<Tuple<int, string>>();
IEnumerable<int> ids = lista.Select(a => a.Item1);

Reference: http://msdn.microsoft.com/en-us/library/bb548891.aspx[^]
and
http://msdn.microsoft.com/en-us/library/bb535050.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900