更新时间:2023-02-14 11:13:41
.NET哪个版本您使用的?如果它是.NET 3.5的,我只是叫 ToArray的()
,并用它做。
Which version of .NET are you using? If it's .NET 3.5, I'd just call ToArray()
and be done with it.
如果你只有一个非通用IEnumerable的,做这样的事情:
If you only have a non-generic IEnumerable, do something like this:
IEnumerable query = ...;
MyEntityType[] array = query.Cast<MyEntityType>().ToArray();
如果你不知道该方法中的类型,但该方法的调用者知道它,使该方法一般和尝试这个办法:
If you don't know the type within that method but the method's callers do know it, make the method generic and try this:
public static void T[] PerformQuery<T>()
{
IEnumerable query = ...;
T[] array = query.Cast<T>().ToArray();
return array;
}