c# - 在 LINQ 中使用命名元组数组

标签 c# linq tuples

我正尝试在 LINQ(LINQ 到对象)查询中使用命名元组数组,如下所示:

(int from, int to)[] inputRanges = { (1,2), (3,4) };

var result = from range in inputRanges
             select Enumerable.Range(range.from, range.to - range.from + 1);
return result.SelectMany(x => x);

但是,我收到一个编译器错误,告诉我 range 没有成员 from 而是需要一个逗号 , 而不是 .来自.

我做错了什么?命名元组和 LINQ-to-objects 不能组合吗?

最佳答案

问题在于 from 是 linq 用于迭代集合项的关键字(from item in collection ...)。

要解决它,请在 linq 中使用 @from:

var result = from range in inputRanges
             select Enumerable.Range(range.@from, range.to - range.@from + 1);

有关 @ 的更多信息,请参阅:C# prefixing parameter names with @ . IMO 在这种情况下使用属性名称 from 是可以的,但我确实认为通常这将是一种不好的做法。

关于c# - 在 LINQ 中使用命名元组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53325882/

相关文章:

c# - 在数字文本框字段中添加零

c# - 通过代理从 Azure Functions 连接到 REST API(c#)

c# - Entity Framework - 生成类

c# - 林克到 SQL : Most Efficient Way of Looping through List of ID's

c++ - 带有成员函数指针的 boost::tuple

python - 如何先根据键对元组元素进行排序,然后根据值对元组元素进行排序

c# - 条件语句中插入 "1==1"(1等于1)有什么实际用途?

c# - 在 linq 查询中调用 c# 方法( Entity Framework )

c# - 如何根据 Dictionary int 值订购 Dictionary<int, string> 列表?

c# - 从列表中返回元组计数