If without ToList(), its type is Iterator. .First().Selected only works for list, it is not avaliable for iterator
Are you sure? Dear Yang yang:-)
VERY GLAD TO SEE YOU HERE AGAIN!!!!!!!!!!!!
I've decompiled the codes for first in reflector, and the codes are——
[__DynamicallyInvokable]
public static TSource First<TSource>(this IEnumerable<TSource> source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
IList<TSource> list = source as IList<TSource>;
if (list != null)
{
if (list.Count > 0)
{
return list[0];
}
}
else
{
using (IEnumerator<TSource> enumerator = source.GetEnumerator())
{
if (enumerator.MoveNext())
{
return enumerator.Current; //Attention here!
}
}
}
throw Error.NoElements();
}
Please notice the bold one——it will return you the Current suitable instance——and the instance should be type of REFER……So any changes to the referred type should change its own value. So I'm afraid yours isn't right.
I tend to believe it that each time when you use First() or something like this, it will render to do query to the L2S or L2O again to generate new object collection. But not very sure……:(
TimoYang
Contributor
3732 Points
1275 Posts
Re: Why it cannot work property without "ToList"?
Jun 04, 2012 07:40 AM|LINK
Are you sure? Dear Yang yang:-)
VERY GLAD TO SEE YOU HERE AGAIN!!!!!!!!!!!!
I've decompiled the codes for first in reflector, and the codes are——
[__DynamicallyInvokable] public static TSource First<TSource>(this IEnumerable<TSource> source) { if (source == null) { throw Error.ArgumentNull("source"); } IList<TSource> list = source as IList<TSource>; if (list != null) { if (list.Count > 0) { return list[0]; } } else { using (IEnumerator<TSource> enumerator = source.GetEnumerator()) { if (enumerator.MoveNext()) { return enumerator.Current; //Attention here! } } } throw Error.NoElements(); }Please notice the bold one——it will return you the Current suitable instance——and the instance should be type of REFER……So any changes to the referred type should change its own value. So I'm afraid yours isn't right.
I tend to believe it that each time when you use First() or something like this, it will render to do query to the L2S or L2O again to generate new object collection. But not very sure……:(
Is that so?
If yes,
m1kael,
kshyju
and
bruce (sqlwo...
are all right!
Waiting for a full-trusted reason……
:D