Best way to convert IList or IEnumerable to Array
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a HQL query that can generate either an IList of results, or an IEnumerable of results. However, I want it to return an array of the Entity that I'm selecting, what would be the best way of accomplishing that? I can either enumerate through it and build the array, or use CopyTo() a defined array. Is there any better way? I went with the CopyTo-approach. 回答1: Which version of .NET are you using? If it's .NET 3.5, I'd just call ToArray() and be done with it. If you only have a non-generic IEnumerable, do something like this: IEnumerable