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

发布于 2024-09-07 06:25:09 字数 914 浏览 1 评论 0原文

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

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;
}

I have a stored procedure which returns all the fields of a table plus one, ie:

tablename.*,count(suchandsuch)

Of course, when executing this via LINQs ExecuteQuery I can get back instances of the table's class in an IEnumerable<>, but I also want the extra field which the stored proc tacks on.

Is this possible?

My current code looks like this:

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

濫情▎り 2024-09-14 06:25:09

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

例如:

  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

One thing you can do is modify your stored procedure and put one output variable in that

for example :

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

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

will resolve your problem because when you drop this procedure in dbml desinger file it will create one out variable by default and will you can easily access in you code

check this for more detail : http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2008/03/13/10236.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文