简体   繁体   English

如何在C#中将Linq查询结果从BLL层返回到UI层

[英]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.

相关问题 从 C# 层 (BLL) 连接到 socket.io - Connect to socket.io from C# layer (BLL) 将C#多层项目部署到测试系统(UI / BLL / DAL / WebService) - Deploy C# multi layer project to a test system (UI/BLL/DAL/WebService) Windows窗体应用程序如何从数据层C#访问UI层中的datagridview - How to access datagridview in UI layer from data layer C# ?windows forms application 使用LINQ-to-Entities将结果从业务层返回到表示层的最佳方法 - Best way to return result from business layer to presentation layer when using LINQ-to-Entities 如何从LINQ查询返回匿名类型的对象(或对象列表)到C#中的UI(控制台应用程序)? - How to return an object (or a list of objects) of anonymous type from a LINQ query to the UI (Console App) in C#? 如何在c#中从BLL访问DataSet - How to access DataSet From BLL in c# ASP.NET 3层/ 3层架构 - 如何分离UI和BLL - ASP.NET 3-Tier / 3-Layer architecture - how to separate UI and BLL 在c#中,是否可以将两个不同的类作为返回类型从一层传递到另一层? - In c#, Is it possible to pass two different classes as return type from one layer to other layer? LINQ查询C#错误的结果 - wrong result from linq query c# BLL 层或单独层中的数据传输对象? - Data Transfer Objects in BLL layer or a separate layer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM