使用 LINQ .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;
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以做的一件事是修改您的存储过程并在其中放入一个输出变量,
例如:
将解决您的问题,因为当您将此过程放入 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 :
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