2 Linq语法之间的区别 [英] Difference between 2 Linq syntax

查看:76
本文介绍了2 Linq语法之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





有什么区别



1. var v1 =(from m in context.Customers选择m);

2. var v1 =(来自上下文中的m。客户选择m).ToList();

解决方案

< blockquote>第二个从查询返回的 IEnumerable 创建一个 List



如果您想强制立即发生项目的枚举,这很方便,因为 Linq 使用推迟执行 [ ^ ]这意味着在枚举项目之前查询未完全执行。



在某些情况下,序列只被枚举一次很重要,在这些情况下,从查询中立即创建列表或数组并返回t非常方便帽子。



希望这会有所帮助,

Fredrik


Hi,

What is difference between

1. var v1 = (From m in context.Customers select m);
2. var v1 = (From m in context.Customers select m).ToList();

解决方案

Second one creates a List from the IEnumerable returned by the query.

This is handy if you want to force the enumeration of the items to happen immediately, as Linq employs deferred execution[^] which means the query isn't fully executed until the items are enumerated.

In some cases it's important that the sequence is only enumerated once, and in those cases it's very convenient to immediately create a list or array from the query and return that.

Hope this helps,
Fredrik


这篇关于2 Linq语法之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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