Calling stored procedures in LINQ-SQL

Source: Internet
Author: User

This question has been asked by the customer many times. I think it is a small one, but it may be helpful to everyone. I will write it down and share it with you by the way.

The problem is that many customers don't know how to accept returned result sets when calling a stored procedure in a LINQ-SQL. So many people have done this. Of course, it seems understandable:

Sampledatacontext Test =   New Sampledatacontext ();
Ienumerable < Tab1 > Retval = Test. Sp (); // Error here!

However, an error occurs during compilation and a type conversion error is reported. In fact, when calling SP in the LINQ-SQL, The LINQ-SQL will automatically generate a type for the returned result set, the type name is composed of SP name + "result. If the SP name is SP, the generated type is spresult. The result set returned when the SP is called is isingleresult <spresult>. As to why a type conversion error is reported, isingleresult <t> and ienumerable <t> cannot convert each other.

Now everyone should know how to do it? Try:Code:

Sampledatacontext Test =   New Sampledatacontext ();
Isinlgeresult < Spresult > Retvalue = Test. Sp ();
Foreach (Spresult Value In Retvalue)
{
String Field = Value. field;

}

Knowing how to do it is far from enough, we suggest you look at the LINQ-SQL automatically generated code. If you have such a habit, you can easily solve the problem yourself. But there is also a small bug in the LINQ-SQL, as singleresult implies, it cannot generate multipleresults for us, such as when SP contains two select statements. If this happens, please leave a message. I will post the solution and start working now :)

Have a nice day!

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.