3

我正在尝试使用 Linq 调用存储过程,存储过程在 SQL 中返回值很好,但是当我将它拖到我的 DBML 文件并尝试从我的代码中调用它时,它返回

找不到源类型“int”的查询模式的实现。未找到“选择”。

我已经查看了我拥有的其他线程和其他存储过程,并且由于某种原因而不是使用ISingleResultthis 是不同的,我似乎也无法更改返回类型。

这是后面的 DBML 代码

[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.displayDetails")]
public int displayDetails([global::System.Data.Linq.Mapping.ParameterAttribute(DbType="VarChar(1)")] string sex, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> day, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> month, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> year, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="PostCode", DbType="VarChar(10)")] string postCode, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="AppTime", DbType="DateTime")] System.Nullable<System.DateTime> appTime, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType="Int")] System.Nullable<int> filter)
{
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), sex, day, month, year, postCode, appTime, filter);
        return ((int)(result.ReturnValue));
}

我的页面代码出现错误

    var person = from p in db.displayDetails(sex.ToString(),
                  Convert.ToInt32(dayOfBirth),
                  Convert.ToInt32(monthOfBirth),
                  Convert.ToInt32(yearOfBirth),
                  postCode.ToString(),
                  Convert.ToDateTime(appointmentTime),
                  Convert.ToInt32(resultType))
                  select person;
        {
            p.Forename,
            p.Surname,
            p.AppointmentTime,
            p.Location
        };

        foreach (var record in person)
        {
            lblName.Text = record.Forename + " " + record.Surname;
            lblAppointmentTime.Text = record.AppointmentTime.ToString();
            lblWaitIn.Text = record.Location;
        }   

任何有关如何解决此问题的帮助将不胜感激。

更新 - 嗨,这是我的 SP:

@sex varchar (1),
@day int,
@month int,
@year int,
@PostCode varchar (10),
@AppTime DateTime,
@filter int

AS
BEGIN

declare @sql nvarchar(max)
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here

Select @Sql = 'SELECT DAY(DateOfBirth) AS DayOfBirth, MONTH(DateOfBirth) AS MonthOfBirth, 
YEAR(DateOfBirth) AS YearOfBirth, DateOfBirth, Sex, AppointmentTime, SchdlRefno, Forename, 
Surname, ClinicCode, Ur, PostCode, Specialty, Location
FROM dbo.tbl_Appointments'


if @filter  = 1
-- show search by just sex, day and month as no more needed for match
Select @sql = @Sql + ' WHERE (DAY(DateOfBirth) = ' + convert(varchar, @day, 103) + ') AND (MONTH(DateOfBirth) = ' + convert(varchar, @month, 103) + ') AND (Sex = ''' + @sex + ''')' 

if @filter  = 2

-- show search by sex, day and month and postcode

Select @sql = @Sql + ' WHERE (DAY(DateOfBirth) = ' + convert(varchar, @day, 103) + ') AND (MONTH(DateOfBirth) = ' + convert(varchar, @month, 103) + ') and (YEAR(DateOfBirth) = ' + convert(varchar, @year, 103) + ') AND (Sex = ''' + @sex + ''') and (postcode = ''' + @postcode + ''')' 

if @filter  = 3

-- show search by sex, day and month, postcode and appointment time

Select @sql = @Sql + ' WHERE (DAY(DateOfBirth) = ' + convert(varchar, @day, 103) + ') AND (MONTH(DateOfBirth) = ' + convert(varchar, @month, 103) + ') and (YEAR(DateOfBirth) = ' + convert(varchar, @year, 103) + ') AND (Sex = ''' + @sex + ''') and (postcode = ''' + @postcode + ''') and (AppointmentTime = ''' + convert(varchar, @AppTime, 121) + ''')' 

print @sql  

Exec sp_executesql  @sql

END

所以,我通过一个过滤器来带回不同的搜索结果。

谢谢

4

1 回答 1

4

The problem is the result of your function isn't a collection, it's a scalar value (int) therefore can't be projected.

于 2012-12-03T12:03:54.827 回答