AsEnumerable 的意义?
var query =
from dt1 in dtStudent.AsEnumerable()
join dt2 in dtMarks.AsEnumerable()
on dt1.Field<int>("StudentID")
equals dt2.Field<int>("StudentID")
select new StudentMark
{
StudentName = dt1.Field<string>("StudentName"),
Mark = dt2.Field<int>("Mark")
};
在上面的编码中,AsEnumerable
的意义是什么?如果 .NET Framework 中不存在 AsEnumerable,那么开发人员执行上述任务的方法是什么?
var query =
from dt1 in dtStudent.AsEnumerable()
join dt2 in dtMarks.AsEnumerable()
on dt1.Field<int>("StudentID")
equals dt2.Field<int>("StudentID")
select new StudentMark
{
StudentName = dt1.Field<string>("StudentName"),
Mark = dt2.Field<int>("Mark")
};
In the above coding, what is the significance of AsEnumerable
? if the AsEnumerable
doesn't exist in .NET Framework, then what would be the approach of developers to perform the above the task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设我正确解释它,它正在调用
DataTableExtensions.AsEnumerable()
。如果没有它(或类似的东西),您就无法使用 LINQ to Objects,因为DataTable
不实现IEnumerable
,仅实现IEnumerable
。请注意,另一种方法是使用
Cast
,但这会略有不同,因为直接使用DataTable
的GetEnumerator
在Cast
内,而我相信EnumerableRowCollection
对数据表做了稍微更时髦的事情。除了可能存在轻微的性能差异外,它不太可能显示出任何真正的变化。Assuming I'm interpreting it correctly, it's calling
DataTableExtensions.AsEnumerable()
. Without that (or something similar), you can't use LINQ to Objects asDataTable
doesn't implementIEnumerable<T>
, onlyIEnumerable
.Note that an alternative would be to use
Cast<DataRow>
, but that would be subtly different as that would use theDataTable
'sGetEnumerator
directly withinCast
, whereas I believeEnumerableRowCollection<TRow>
does slightly more funky things with the data table. It's unlikely to show up any real changes, except possibly a slight performance difference..AsEnumerable()
扩展只是将实现IEnumerable
的东西强制转换为IEnumerable
所以,如果
xs
是int[]
,您可以调用xs.AsEnumerable()
而不是(xs as IEnumerable)
。它使用类型推断来避免需要显式键入xs
的类型。以下是 Reflector.NET 提取的代码:
但在这种情况下,我想我必须同意 Jon 的观点。它可能来自 System.Data.DataSetExtensions 程序集。
The
.AsEnumerable()
extension is just short-hand for casting something that implementsIEnumerable<T>
to beIEnumerable<T>
So, if
xs
isint[]
, you can callxs.AsEnumerable()
instead of(xs as IEnumerable<int>)
. It uses type inference to avoid needing to explicitly keying the type ofxs
.Here's the code extracted by Reflector.NET:
But in this case I think I have to agree with Jon. It's probably from the
System.Data.DataSetExtensions
assembly.