[英]simple LINQ refactoring / style
As LINQ Select()
wants a FUNC<int,T>
as argument, I have to define an unused variable. 由于LINQ
Select()
需要FUNC<int,T>
作为参数,因此我必须定义一个未使用的变量。 I think query syntax makes that clearer in this case. 我认为查询语法在这种情况下更加清楚。
How do I get rid of any clutter to make that simple and clean to read? 我如何摆脱任何混乱,使阅读变得简单整洁?
var rand = new Random();
//Method syntax
IEnumerable<char> c = Enumerable.Range(0, 10)
.Select( (p) => (char)('A' + rand.Next(0,26)));
//Query syntax
var c = from counter in Enumerable.Range(0,10)
select (char)('A' + rand.Next(0,26));
It looks pretty clean and easy to read already. 它看起来很干净,而且易于阅读。
I don't think you'll get any further, nor that it's worthwhile -- even if you define your own Select<T, TR>(this T t, Func<TR> f)
, you'll still have to call it as t.Select(() => ...)
(notice the empty parentheses). 我认为您不会再进一步了,也不值得-即使您定义自己的
Select<T, TR>(this T t, Func<TR> f)
,您仍然必须调用它如t.Select(() => ...)
(注意空括号)。
You can explicitly mark an unused variable as _
though: 您可以将未使用的变量显式标记为
_
:
var c = Enumerable.Range(0, 10).Select(_ => (char)('A' + rand.Next(0, 26)));
Or, you can use an added abstraction and hide the projection, say, in a local method:) 或者,您可以使用添加的抽象并在局部方法中隐藏投影:)
char RandomChar(int _) => (char)('A' + rnd.Next(0, 26));
var c = Enumerable.Range(0, 10).Select(RandomChar);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.