Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET4.0
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 18-Jan-13 3:30am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  

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

  Print Answers RSS
0 Sascha Lefévre 365
1 Sergey Alexandrovich Kryukov 325
2 Maciej Los 240
3 Abhinav S 184
4 Peter Leow 120
0 Sergey Alexandrovich Kryukov 6,773
1 OriginalGriff 6,311
2 Maciej Los 2,692
3 Peter Leow 2,654
4 Abhinav S 2,562


Advertise | Privacy | Mobile
Web01 | 2.8.150414.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100