如何更好地写这个?
让我们看看这段代码:
IList<IHouseAnnouncement> list = new List<IHouseAnnouncement>();
var table = adapter.GetData(); //get data from repository object -> DataTable
if (table.Rows.Count >= 1)
{
for (int i = 0; i < table.Rows.Count; i++)
{
var anno = new HouseAnnouncement();
anno.Area = float.Parse(table.Rows[i][table.areaColumn].ToString());
anno.City = table.Rows[i][table.cityColumn].ToString();
list.Add(anno);
}
}
return list;
是否有更好的方法以更少的代码和更好的方式编写此代码(必须是:-))?也许使用 lambda (但让我知道如何)?
提前致谢!
Let's look at this code:
IList<IHouseAnnouncement> list = new List<IHouseAnnouncement>();
var table = adapter.GetData(); //get data from repository object -> DataTable
if (table.Rows.Count >= 1)
{
for (int i = 0; i < table.Rows.Count; i++)
{
var anno = new HouseAnnouncement();
anno.Area = float.Parse(table.Rows[i][table.areaColumn].ToString());
anno.City = table.Rows[i][table.cityColumn].ToString();
list.Add(anno);
}
}
return list;
Is it better way to write this in less code and better fashion (must be :-) )? Maybe using lambda (but let me know how)?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
仅供参考,您永远不会将新的
HouseAnnouncement
添加到列表中,并且您的循环永远不会针对 last 行执行,但我假设这些是示例而不是在您的实际代码中。你可以这样做:
我通常追求可读性而不是简洁性,但我觉得这是非常可读的。
请注意,虽然您仍然可以缓存
DataTable
并在 lambda 中使用table.powierzchniaColumn
,但我消除了这一点,这样您就不会使用不必要的闭包(闭包给 lambda 的内部实现带来了相当大的复杂性,所以如果可能的话我会避免使用它们)。如果保持列引用原样对您来说很重要,那么您可以这样做:
这会增加编译器生成的实际 IL 的复杂性,但应该可以满足您的要求。
Just FYI, you're never adding the new
HouseAnnouncement
to your list, and your loop will never execute for the last row, but I'm assuming those are errors in the example rather than in your actual code.You could do something like this:
I usually go for readability over brevity, but I feel like this is pretty readable.
Note that while you could still cache the
DataTable
and usetable.powierzchniaColumn
in the lambda, I eliminated that so that you didn't use a closure that wasn't really necessary (closures introduce substantial complexity to the internal implementation of the lambda, so I avoid them if possible).If it's important to you to keep the column references as they are, then you can do it like this:
This will add complexity to the actual IL that the compiler generates, but should do the trick for you.
你可以在 Linq 中做这样的事情:
You could do something like this in Linq:
你的“if 语句”是不必要的。您的“for 循环”已经处理了这种情况。
另外,当表行数为 1 时,“for 循环”将不会执行。这似乎是一个错误,而且不是设计使然,但我可能是错的。如果你想解决这个问题,只需去掉“-1”:
Your "if statement" is not necessary. Your "for loop" already takes care of that case.
Also, your "for loop" will not execute when the number of your Table Rows is 1. This seems like a mistake, and not by design, but I could be wrong. If you want to fix this, just take out the "-1":
好吧,一方面,您似乎遇到了差一错误:
如果您的表有三行,则当
i
小于 3 - 1 或 2 时,这将运行,这意味着它将针对第 0 行和第 1 行运行,但不会针对第 2 行运行。这可能不是您想要的。Well, for one thing, you appear to have an off-by-one error:
If your table has three rows, this will run while
i
is less than 3 - 1, or 2, which means it'll run for rows 0 and 1 but not for row 2. This may not be what you intend.没有比一个 for 循环和没有 if 语句更简单的了:
它比 linq 版本需要更多的字符,但程序员的大脑解析速度更快。 :)
Can't go much simpler that one for-loop and no if-statements:
It takes more characters than linq-version, but is parsed faster by programmer's brain. :)
对我来说,可读性比代码简洁更好——只要性能不是牺牲品。另外,我相信任何以后需要维护代码的人也会欣赏它。
即使当我维护自己的代码时,我也不想在几个月后查看它并思考“我到底想完成什么”
Readability is, to me, preferable to being succinct with your code--as long as performance is not a victim. Also, I am sure that anyone who later has to maintain the code will appreciate it as well.
Even when I am maintaining my own code, I don't want to look at it, say a couple of months later, and think "what the hell was I trying to accomplish"
我可能会做这样的事情:
I might do something like this: