DBML:
[Function (Name="Dbo.p_getstudystagesubjectgroup")] PublicIsingleresult<studystage_subjectgroup__qrm> P_getstudystagesubjectgroup ([Parameter (Name ="timepoints", DbType ="DateTime")] System.nullable<system.datetime>timepoints) {Iexecuteresult result= This. Executemethodcall ( This, ((MethodInfo) (Methodinfo.getcurrentmethod ())), timepoints); return((isingleresult<studystage_subjectgroup__qrm>) (Result. returnvalue)); }
View Code
Background. CS Page:
New Datacontextsetup ();isingleresult<studystage_subjectgroup__qrm> result = Context.p_getstudystagesubjectgroup (DateTime.Now); this. Listview1.datasource = result; this. Listview1.databind ();
View Code
Run Error: "Cannot enumerate query results multiple times. ”
Check out the foreign data found, because the isingleresult<t> output of LINQ to SQL cannot be enumerated more than 1 times.
For example, when you bind isingleresult<t> to a drop-down box list, the result is enumerated and the drop-down box adds data, and then if you bind the same isingleresult<t> instance to the same control again, It will find that its data source is the same as the previous one (through the object reference), and it will not enumerate the isingleresult<t> instances again.
To solve this problem, there are two ways available:
1, to achieve their own isingleresult<t>
Public classWrapperresult<t>: isingleresult<t> { #regionFieldsList<T>results; Objectreturnvalue; #endregion #regionConstructor PublicWrapperresult (isingleresult<t>inner) {Results=Inner. ToList (); ReturnValue=Inner. returnvalue; } #endregion #regionIenumerable<t> Members PublicIenumerator<t>GetEnumerator () {returnresults. GetEnumerator (); } #endregion #regionIEnumerable MembersIEnumerator Ienumerable.getenumerator () {return((IEnumerable) results). GetEnumerator (); } #endregion #regionIfunctionresult Members Public Objectreturnvalue {Get{returnreturnvalue;} } #endregion #regionIDisposable Members Public voidDispose () {}#endregion }
View Code
Detailed Visibility HTTP://SOCIAL.MSDN.MICROSOFT.COM/FORUMS/EN/LINQPROJECTGENERAL/THREAD/F403E047-E38C-45AB-9155-DBF83C6CC2FA
2. Convert isingleresult<t> directly to .tolist<t> ()
New Datacontextsetup (); list<studystage_subjectgroup__qrm> result = Context.p_getstudystagesubjectgroup (DateTime.Now). ToList (); this. Listview1.datasource = result; this. Listview1.databind ();
View Code
"Cannot enumerate query results multiple times" issue with LINQ to SQL call stored procedure