将 Linq 与二维数组一起使用,找不到选择 [英] Using Linq with 2D array, Select not found
问题描述
我想使用 Linq 查询二维数组,但出现错误:
I want to use Linq to query a 2D array but I get an error:
找不到源类型SimpleGame.ILandscape[,]"的查询模式的实现.未找到选择".您是否缺少对System.Core.dll"的引用或System.Linq"的 using 指令?
Could not find an implementation of the query pattern for source type 'SimpleGame.ILandscape[,]'. 'Select' not found. Are you missing a reference to 'System.Core.dll' or a using directive for 'System.Linq'?
代码如下:
var doors = from landscape in this.map select landscape;
我已经检查过我是否包含了参考 System.Core
并使用了 System.Linq
.
I've checked that I included the reference System.Core
and using System.Linq
.
谁能给出一些可能的原因?
Could anyone give some possible causes?
推荐答案
为了在 LINQ 中使用多维数组,您只需将其转换为 IEnumerable
.很简单,这里有两个查询的示例选项
In order to use your multidimensional array with LINQ, you simply need to convert it to IEnumerable<T>
. It's simple enough, here are two example options for querying
int[,] array = { { 1, 2 }, { 3, 4 } };
var query = from int item in array
where item % 2 == 0
select item;
var query2 = from item in array.Cast<int>()
where item % 2 == 0
select item;
每种语法都会将二维数组转换为 IEnumerable
(因为您在 from 子句或 array.Cast
在其他).然后,您可以使用 LINQ 方法过滤、选择或执行您希望的任何投影.int item
()
Each syntax will convert the 2D array into an IEnumerable<T>
(because you say int item
in one from clause or array.Cast<int>()
in the other). You can then filter, select, or perform whatever projection you wish using LINQ methods.
这篇关于将 Linq 与二维数组一起使用,找不到选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!