"Cannot enumerate query results multiple times" issue with LINQ to SQL call stored procedure

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.