如何转换为LINQ [英] How to Convert to LINQ

查看:69
本文介绍了如何转换为LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



请参阅以下内容:



Hello,

Please see below:

int i = 0;
           foreach (DataGridViewRow dgr in this.dataGridView1.Rows)
           {
               dgr.DefaultCellStyle.BackColor = ((i % 2) == 0) ? Color.LightGray : Color.DarkGray;
               i++;
           }





有人能告诉我如何将上面的代码转换成LINQ吗?



非常感谢提前



Can anybody tell me how the above code can be converted into LINQ please?

Many thanks in advance

推荐答案

我能想出的最佳解决方案是



购买ReSharper,并且有一个功能可以将foreach转换为LINQ。



这就是我过去使用过的,现在想念它:(



http://www.jetbrains.com/resharper/ [ ^ ]
Best solution I can come up with is

Buy ReSharper, and there is a function to convert foreach into LINQ.

That''s what I have used in the past, and miss it now :(

http://www.jetbrains.com/resharper/[^]


首先,为什么在LINQ中执行此操作?如果没有它,您所拥有的代码就完成了工作,所以我建议您查看为什么需要更改。



如果你愿意,你可以清理一下:

First, why do this in LINQ? The code you have does the job without it, so I''d suggest taking a look at why it is necessary to change.

If you want to you could clean you line a little:
bool isOddRow = true;
foreach (DataGridViewRow dgr in this.dataGridView1.Rows)
{
    dgr.DefaultCellStyle.BackColor = isOddRow ? Color.LightGray : Color.DarkGray;
    isOddRow = !isOddRow;
}





你可以做的一个积极的改变是利用 AlternatingRowsDefaultCellStyle RowsDefaultCellStyle ,这可能完全使上述冗余,请参阅MSDN示例。



One positive change you could make is to take advantage of AlternatingRowsDefaultCellStyle and RowsDefaultCellStyle, which would probably make the above redundant totally, see MSDN for examples.


这篇关于如何转换为LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆