[英]How to return Linq query result from BLL layer to UI layer in C#
I'm new to .NET framework. 我是.NET框架的新手。 I am working on a 3 tier ASP.NET application that uses Linq to SQL for data access.
我正在使用Linq to SQL进行数据访问的3层ASP.NET应用程序。 I don't know how to return a Linq to SQL result to the UI layer.
我不知道如何将Linq to SQL结果返回到UI层。 ON the BLL, I have a code like this:
在BLL上,我有这样的代码:
dataContex db = new dataContex;
var data = db.DB_TBL;
I obviously cannot return var
type. 我显然不能返回
var
类型。 How do I return the query result to the UI layer? 如何将查询结果返回到UI层?
You can return db.DBL_TBL which will return IQueriable. 您可以返回db.DBL_TBL,它将返回IQueriable。 For example,
例如,
public IQueriable<DB_TBL> GetTable()
{
dataContex db = new dataContex;
return db.DB_TBL
}
You can also return IEnumerable or List as well. 您也可以返回IEnumerable或List。 See example below which returns List.
请参阅下面的示例,该示例返回List。
public List<DB_TBL> GetTable()
{
dataContex db = new dataContex;
return db.DB_TBL.ToList();
}
Then in your UI, use the return types. 然后在您的UI中,使用返回类型。
Actually binding to a control like a repeater still happens server side. 实际上,绑定到类似中继器的控件仍然发生在服务器端。 To send your data directly to UI take a look on JSON.
要将数据直接发送到UI,请查看JSON。 It is a better way dealing with data transport (you do transport only for data as opposed to data+markup as it happens by using server side controls).
这是处理数据传输的更好方法(您只传输数据,而不是使用服务器端控件进行数据+标记传输)。
And since you are new I would recommend working with data on UI, simply it gives you a lot of flexibility. 而且,由于您是新手,所以我建议您在UI上使用数据,因为它可以为您提供很大的灵活性。 And you keep the code clean.
而且您可以保持代码干净。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.