你的问题有点含糊,所以我不确定这是否会有所帮助,但我只是使用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