使用 linq 时从 WCF 服务返回什么?

发布于 2024-09-15 17:32:00 字数 308 浏览 2 评论 0原文

使用 LINQ 时,我应该从 WCF 服务返回什么?例如:

var res = from q in context.cust
          select q;

LINQ 遵循延迟执行,因此在运行 for 循环之前该语句不会执行任何操作。这意味着我不能只返回 res.那我应该返回什么呢?每次我想从 WCF 服务返回数据时,是否需要编写一个 for 循环并填充对象并返回它的列表?是否没有 ADO.NET DataSet 的等效项,它遵循断开连接的体系结构,并且非常适合在不同层之间以及从 Web 服务或 WCF 服务移动数据?

What should i return from a WCF service when using LINQ? e.g.:

var res = from q in context.cust
          select q;

The LINQ follows Deferred Execution and thus the statement doesn't do anything until a for loop is ran. This means i can't just return res. Then what should i return? Do i need to write a for loop and populate objects and return it's List every time i want to return data from WCF service? Is there no equivalent of ADO.NET DataSet which follows disconnected architecture and is ideal for moving data between different tiers and from web service or WCF service?

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

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

发布评论

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

评论(3

安静被遗忘 2024-09-22 17:32:00

执行被推迟到实际请求数据为止,您是对的,这可能是一个 for 循环,但也可能是在需要序列化以通过 WCF 传输时。如果您使 WCF 服务返回 IEnumerable,则将执行查询并返回结果。

Execution is deferred until the data is actually requested, you're right that that may be a for loop but it would also be when it needs to be serialized for transfer via WCF. If you make your WCF service return IEnumerable then the query will be executed and the results returned.

对岸观火 2024-09-22 17:32:00

如果将其返回为 IEnumerable,WCF 序列化程序应执行查询。

但我通常这样做

var res = (from q in context.cust select q).ToList();

If you return it as IEnumerable<cust> the WCF serializer should execute the query.

But I usually do

var res = (from q in context.cust select q).ToList();
客…行舟 2024-09-22 17:32:00

由于查询中提到的上下文,您必须通过将结果转换为数组或列表来显式执行查询。上下文可能与 Linq-To-Sql 或 EF 相关,并且在序列化期间,延迟执行很有可能失败,因为上下文已经关闭。

Because of context mentioned in your query you have to execute query explicitly by converting result to array or list. Context is probably related to Linq-To-Sql or EF and there is a big chance that during serialization the deffered execution fails because the context will be already closed.

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