你的問題有點含糊,所以我不確定這是否會有所幫助,但我只是使用AdventureWorks db進行了一次小測試,它似乎工作 - 我會發布我的測試代碼,也許它會給你是一個線索。 (這只是取出由Employee表3列到一個新的/自定義 「EMPTEST」 級)。
存儲過程:
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Get_Employee]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Get_Employee]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE Get_Employee
@id int = 0
AS
BEGIN
SET NOCOUNT ON;
SELECT EmployeeID, Title, VacationHours from HumanResources.Employee where EmployeeID = @id
END
GO
的LINQ到SQL存儲過程的方法(把這個在您的DataContext類):
[Function(Name="dbo.Get_Employee")]
public ISingleResult<EmpTest> Get_Employee([Parameter(DbType="Int")] System.Nullable<int> id)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), id);
return ((ISingleResult<EmpTest>)(result.ReturnValue));
}
自定義實體:
[Table(Name = "HumanResources.Employee")]
public class EmpTest
{
private int _EmployeeID;
private string _Title;
private short _VacationHours;
[Column(Storage = "_EmployeeID", DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
public int EmployeeID
{
get { return this._EmployeeID; }
set { this._EmployeeID = value; }
}
[Column(Storage = "_Title", DbType = "NVarChar(50) NOT NULL", CanBeNull = false)]
public string Title
{
get { return this._Title; }
set { this._Title = value; }
}
[Column(Storage = "_VacationHours", DbType = "SmallInt NOT NULL")]
public short VacationHours
{
get { return this._VacationHours; }
set { this._VacationHours = value; }
}
}
並且,最後通話:
EmpTest emp = context.Get_Employee(41).SingleOrDefault();
爲什麼工作這麼難?如果你聲明一個臨時表,linq to sql將爲你創建一個自定義結果集! – Yuki 2013-10-03 08:27:08