Linq to SQL Stored Procedure Composite Type
One of the good things I learnt while I was working over Hibernate in my early days with Java was to extensively use wrappers once you have fetched the required custom business objects from database. However on .NET platform, when I was trying to fetch a rather composite (with data from two tables) type from a stored procedure, I came across a problem, and intitutively, the solution that I came up with was far more elegent (atleast for myself) than other recomended on the web.You see, I had this table structure, and the output that I needed from the stored procedure can be well understood from,
@CategoryId int
as
BEGIN
SET NOCOUNT ON;
select a.AnswerId,a.FUID,a.PostedDate,c.Answer
from dbo.UserAnswers as a, dbo.tblCategoryAnswers as c
where a.CategoryId = @CategoryId and a.AnswerId = c.AnswerId
order by a.PostedDate descENDNow when I compiled my class library containing the dbml file, the output function yielded by Visual Studio was returning an Integer value. That was quite an expected behavior from Visual Studio. <FunctionAttribute(Name:="dbo.GetAllUserAnswersAgainstCategory")>
_GetAllUserAnswersAgainstCategory(<Parameter(Name:="CategoryId", DbType:="Int")>
ByVal categoryId As System.Nullable(Of Integer)) As Integer
Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, CType(MethodInfo.GetCurrentMethod, MethodInfo), categoryId)
Return CType(result.ReturnValue, Integer)
End FunctionSo what I did was, I changed the return type of function to this, <FunctionAttribute(Name:="dbo.GetAllUserAnswersAgainstCategory")>
_Public Function GetAllUserAnswersAgainstCategory(<Parameter(Name:="CategoryId", DbType:="Int&amp")>
ByVal categoryId As System.Nullable(Of Integer)) As ISingleResult(Of GetAllUserAnswersAgainstCategoryResult)
Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, CType(MethodInfo.GetCurrentMethod, MethodInfo), categoryId)
Return CType(result.ReturnValue, ISingleResult(Of GetAllUserAnswersAgainstCategoryResult))
End Functionand declared a class manually inside the .dbml designer file that exhibits the values returned from my stored procedure.Partial Public Class GetAllUserAnswersAgainstCategoryResult Private _AnswerId As Integer
Private _Answer As String
Private _FUID As Integer
Private _PostedDate As System.Nullable(Of Date) Public Sub New()
MyBase.New()
End Sub <Column(Storage:="_AnswerId", DbType:="Int NOT NULL")> _
Public Property AnswerId() As Integer
Get
Return Me._AnswerId
End Get
Set(ByVal value As Integer)
If ((Me._AnswerId = value) _ = False) Then
Me._AnswerId = value
End If
End Set
End Property <Column(Storage:="_Answer", DbType:="VarChar(1024) NOT NULL", CanBeNull:=False)> _
Public Property Answer() As String
Get
Return Me._Answer
End Get Set(ByVal value As String)
If (String.Equals(Me._Answer, value) = False) Then
Me._Answer = value
End If
End Set
End Property <Column(Storage:="_FUID", DbType:="Int NOT NULL")> _
Public Property FUID() As Integer
Get
Return Me._FUID
End Get
Set(ByVal value As Integer)
If ((Me._FUID = value) _ = False) Then
Me._FUID = value
End If End Set
End Property <Column(Storage:="_PostedDate", DbType:="DateTime")> _
Public Property PostedDate() As System.Nullable(Of Date)
Get
Return Me._PostedDate
End Get Set(ByVal value As System.Nullable(Of Date))
If (Me._PostedDate.Equals(value) = False) Then
Me._PostedDate = value
End If
End Set
End Property
End Class
Compiled and I was getting the required values from my composite type or wrapper class upon each iteration.
Posted
by Usman ur Rehman Ahmed
About Me
Usman ur Rehman Ahmedis known as a software engineer in Lahore, Pakistan. He is renowned for having an abstract understanding of vast range of technological developments such as programming languages, web development, RIA's and documental writing, and is mainly specialized in developmental analysis.
Tags
- windows phone 7 (15)
- DNN (14)
- Dot Net Nuke (13)
- .NET (5)
- c# (5)
- manifest (4)
- Application (3)
- MySql (3)
- Provider (3)
- Sql (3)
- View all 237 tags »
- Sql Server (3)
- Stored Procedure (3)
- visual sutdio (3)
- API (2)
- Data (2)
- Mango (2)
- Module (2)
- Permission (2)
- PhoneTextBlock (2)
- User Control (2)
- Windows (2)
- config (2)
- console (2)
- developer (2)
- dot net (2)
- emulator (2)
- enumeration (2)
- expression blend (2)
- extension (2)
- loop (2)
- network (2)
- phone 7 (2)
- tools (2)
- transparent (2)
- .ascx (1)
- 3g (1)
- 5.6.2 (1)
- 7.1 (1)
- 7.5 (1)
- AJAX (1)
- Activity (1)
- App.xaml (1)
- AppManifest (1)
- ApplicationIcon (1)
- Arabic (1)
- Background.png (1)
- BuildAction (1)
- Connector (1)
- Control Panel (1)
- Core Version (1)
- Data-tier (1)
- DataBound (1)
- Database (1)
- Dependency (1)
- DeviceNetworkInterface (1)
- Directory (1)
- Dynamic (1)
- EVDO (1)
- Encryption (1)
- File (1)
- FileSystemWatcher (1)
- Filter (1)
- Foreign Key (1)
- Function (1)
- GUID (1)
- GetSiteLog (1)
- Hash (1)
- Host Settings (1)
- IIS6 (1)
- IIS7 (1)
- Integrated Mode (1)
- Internet Explorer (1)
- IsolatedStorage (1)
- Java (1)
- Koder (1)
- Linq (1)
- Lists (1)
- Log (1)
- Marketplace (1)
- Membership (1)
- Microsoft (1)
- NavigationCacheMode (1)
- NavigationContext (1)
- NavigationService (1)
- Notify (1)
- OnClientNodeChecked (1)
- PTCL (1)
- Panaroma (1)
- Password (1)
- Properties (1)
- Query String (1)
- ResXResourceReader (1)
- ResolveHostNameAsync (1)
- Right to Left (1)
- SDK (1)
- SEO (1)
- SendKeys (1)
- SendWait (1)
- SharpPCap (1)
- Stream (1)