c# - 使用 LINQ .ExecuteQuery 调用存储过程以返回非映射字段

标签 c# linq stored-procedures executequery

我有一个存储过程,它返回一个表的所有字段加一个,即:

tablename.*,count(suchandsuch)

当然,当通过 LINQs ExecuteQuery 执行此操作时,我可以在 IEnumerable<> 中取回表类的实例,但我还想要存储过程附加的额外字段。

这可能吗?

我当前的代码如下所示:

public class CategoryWithInstanceCount : DataAccess.Category
{
    [System.Data.Linq.Mapping.Column(Storage = "_Instances", DbType = "Int NOT NULL")]
    public int Instances { get; set; }
}

protected IEnumerable<CategoryWithInstanceCount> GetCategories(int? parentid)
{
    PriceDataContext context = new PriceDataContext(ConfigurationManager.ConnectionStrings["connStr"].ConnectionString);
    IEnumerable<CategoryWithInstanceCount> ie = context.ExecuteQuery<CategoryWithInstanceCount>(
        String.Format("EXEC [dbo].[GetCategoryFromParentID] @parentcategoryid = {0}", parentid == null ? "NULL" : parentid.ToString())
        );
    return ie;
}

最佳答案

您可以做的一件事是修改您的存储过程并在其中放置一个输出变量

例如:

  create procedure name 
    @var1 int,
    @var2 int output,
   as
  begin

   select @var2=count (columnname), * from tablename
  end

将解决您的问题,因为当您将此过程放入 dbml desinger 文件中时,它会默认创建一个输出变量,您是否可以在代码中轻松访问

查看更多详细信息:http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2008/03/13/10236.aspx

关于c# - 使用 LINQ .ExecuteQuery 调用存储过程以返回非映射字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3148565/

相关文章:

sql - "Stored Procedure has too many arguments specified"SQL服务器

sql - 我们可以在 SQL Server 中使用 while 循环将记录存储在临时表中吗?

c# - 类自身内部的实例

c# - 在 ViewModel 中创建 KeyBinding 时添加参数

c# - LINQ 中子查询的随机 Orderby

c# - 将 IEnumerable<T> 转换为 List<T> 性能

linq - 如何转换此 linq 表达式?

更新 (ALTER) 另一个函数/过程的函数/过程

c# - 哈希表中的列表丢失所有条目

c# - 将 JSON(文本文件)读入 .NET 应用程序