linq与使用3层的商店程序 [英] linq with store procedure using 3 tier

查看:79
本文介绍了linq与使用3层的商店程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用商店采购和3层Archtecure工作linq,

但我有一个问题,



我使用商店procdure喜欢:

i work linq with store procure and 3 tier archtecure,
but i have a problem,

I use store procdure like:

ALTER PROCEDURE [dbo].[SP_Category_Get]
(
             @id int=0

)
As
SET NOCOUNT ON
if(@id=0)
Begin
         Select * From tbl_admin_category


End

else
Begin
 Select * From tbl_admin_category
 where
  id= @id

  End
SET NOCOUNT OFF









当我使用这个程序直接代码背后:

那么它将非常容易:





When i use this procedure direct at code behind:
Then it will be very easy like:

LinqConnectionDataContext li = new LinqConnectionDataContext();
       var v = li.SP_Category_Get(0);

       gd_cat.DataSource = v;
       gd_cat.DataBind();







现在问题是:

当我在BLL调用此商店采购然后我需要定义我在显示页面使用的每个coloumn名称,所以我的代码非常冗长,

所以如果有任何方法可以解决此问题我想要:



BLL上的代码是:






Now the Problem is:
when i call this store procure at BLL then i need to define every coloumn name which i use at display page, so my code is very lengthy,
so i want that if there are any method for removing this problem:

Code On BLL is:

public IEnumerable<tbl_admin_category> GetCategoryDetails(int id)
    {

        LinqConnectionDataContext li = new LinqConnectionDataContext();
        var v = li.SP_Category_Get(id);

        return (from c in v
                select new tbl_admin_category()
                {
                    id = c.id,
                    category = c.category,
                    category_img = c.category_img
                }
               ).ToList();


    } 





然后在Aspx.cs页面:





and then at Aspx.cs Page:

adminBal obj = new adminBal();

       var v = obj.GetCategoryDetails(0);
       gd_cat.DataSource = v;
       gd_cat.DataBind();











所以我想要那个,

1.就像第一个代码一样(当我在aspx.cs页面上调用Direct SP时,我不需要定义coloum名称)

2.我不想在BLL定义coloum名称;







如果我尝试在BLL喜欢这个:

然后它也会产生错误:






so i want that,
1. just like first code(when i call Direct SP at aspx.cs page,and i don''t need to define coloum name)
2. i dont want to define coloum name at BLL;



And If I try at BLL like This:
Then it also produce Error:

public IEnumerable<tbl_admin_category> GetCategoryDetails(int id)
   {


       var v = li.SP_Category_Get(id);

       return v;


   }







Error is:
Cannot implicitly convert type 'System.Data.Linq.ISingleResult' to 'System.Collections.Generic.IEnumerable<tbl_admin_category>'. An explicit conversion exists (are you missing a cast?)

推荐答案

正确。这是你应该得到的例外。 li.SP_Category_Get(id)方法肯定不会返回 IEnumerable< tbl_admin_category> 。结果类型取决于您的模型。所以首先修改你的代码(或制作一个简单的控制台应用程序进行测试,甚至更好地使用 LinqPad [ ^ ])所以你可以运行它并在 var v = li.SP_Category_Get之后放置一个断点( id); 并查看 v 将获得的类型。将该类型用作方法的返回类型。
Correct. This is the exception you should get. The li.SP_Category_Get(id) method won''t return IEnumerable<tbl_admin_category> for sure. The result type will depend on your model. So first modify your code (or make a simple console app to test, or even better use LinqPad[^]) so you can run it and place a breakpoint after var v = li.SP_Category_Get(id); and see what type v will get. Use that type as return type for your method.


这篇关于linq与使用3层的商店程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆