如何更好地写这个?

发布于 2024-08-28 14:04:36 字数 603 浏览 3 评论 0原文

让我们看看这段代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

沒落の蓅哖 2024-09-04 14:04:36

仅供参考,您永远不会将新的 HouseAnnouncement 添加到列表中,并且您的循环永远不会针对 last 行执行,但我假设这些是示例而不是在您的实际代码中。

你可以这样做:

return adapter.GetData().Rows.Cast<DataRow>().Select(row =>
    new HouseAnnouncement()
    {
        Area = Convert.ToSingle(row["powierzchnia"]),
        City = (string)row["miasto"],
    }).ToList();

我通常追求可读性而不是简洁性,但我觉得这是非常可读的。

请注意,虽然您仍然可以缓存 DataTable 并在 lambda 中使用 table.powierzchniaColumn,但我消除了这一点,这样您就不会使用不必要的闭包(闭包给 lambda 的内部实现带来了相当大的复杂性,所以如果可能的话我会避免使用它们)。

如果保持列引用原样对您来说很重要,那么您可以这样做:

using (var table = adapter.GetData())
{
    return table.Rows.Cast<DataRow>().Select(row =>
        new HouseAnnouncement()
        {
            Area = Convert.ToSingle(row[table.powierzchniaColumn]),
            City = (string)row[table.miastoColumn],
        }).ToList();
}

这会增加编译器生成的实际 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:

return adapter.GetData().Rows.Cast<DataRow>().Select(row =>
    new HouseAnnouncement()
    {
        Area = Convert.ToSingle(row["powierzchnia"]),
        City = (string)row["miasto"],
    }).ToList();

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 use table.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:

using (var table = adapter.GetData())
{
    return table.Rows.Cast<DataRow>().Select(row =>
        new HouseAnnouncement()
        {
            Area = Convert.ToSingle(row[table.powierzchniaColumn]),
            City = (string)row[table.miastoColumn],
        }).ToList();
}

This will add complexity to the actual IL that the compiler generates, but should do the trick for you.

追风人 2024-09-04 14:04:36

你可以在 Linq 中做这样的事情:

var table = adapter.GetData();
var q = from row in table.Rows.Cast<DataRow>()
        select new HouseAnnouncement() 
           { Area = float.Parse(row[table.areaColumn].ToString()),
             City = row[table.cityColumn].ToString()
           };
return q.ToList();

You could do something like this in Linq:

var table = adapter.GetData();
var q = from row in table.Rows.Cast<DataRow>()
        select new HouseAnnouncement() 
           { Area = float.Parse(row[table.areaColumn].ToString()),
             City = row[table.cityColumn].ToString()
           };
return q.ToList();
请别遗忘我 2024-09-04 14:04:36

你的“if 语句”是不必要的。您的“for 循环”已经处理了这种情况。

另外,当表行数为 1 时,“for 循环”将不会执行。这似乎是一个错误,而且不是设计使然,但我可能是错的。如果你想解决这个问题,只需去掉“-1”:

for (int i = 0; i < table.Rows.Count; i++)

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":

for (int i = 0; i < table.Rows.Count; i++)
尹雨沫 2024-09-04 14:04:36

好吧,一方面,您似乎遇到了差一错误:

for (int i = 0; i < table.Rows.Count - 1; i++)
{
}

如果您的表有三行,则当 i 小于 3 - 1 或 2 时,这将运行,这意味着它将针对第 0 行和第 1 行运行,但不会针对第 2 行运行。这可能不是您想要的。

Well, for one thing, you appear to have an off-by-one error:

for (int i = 0; i < table.Rows.Count - 1; i++)
{
}

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.

撩心不撩汉 2024-09-04 14:04:36

没有比一个 for 循环和没有 if 语句更简单的了:

var table = adapter.GetData(); //get data from repository object -> DataTable
IList<IHouseAnnouncement> list = new List<IHouseAnnouncement>(table.Rows.Count);

for (int i = 0; i < list.Length; i++)
{
   list[i] = new HouseAnnouncement();
   list[i].Area = float.Parse(table.Rows[i][table.areaColumn].ToString());
   list[i].City = table.Rows[i][table.cityColumn].ToString();
}

return list;

它比 linq 版本需要更多的字符,但程序员的大脑解析速度更快。 :)

Can't go much simpler that one for-loop and no if-statements:

var table = adapter.GetData(); //get data from repository object -> DataTable
IList<IHouseAnnouncement> list = new List<IHouseAnnouncement>(table.Rows.Count);

for (int i = 0; i < list.Length; i++)
{
   list[i] = new HouseAnnouncement();
   list[i].Area = float.Parse(table.Rows[i][table.areaColumn].ToString());
   list[i].City = table.Rows[i][table.cityColumn].ToString();
}

return list;

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"

打小就很酷 2024-09-04 14:04:36

我可能会做这样的事情:

var table = adapter.GetData(); //get data from repository object -> DataTable

return table.Rows.Take(table.Rows.Count-1).Select(row => new HouseAnnouncement() {
    Area = float.Parse(row[table.powierzchniaColumn].ToString()),
    City = row[table.miastoColumn].ToString()
}).ToList();

I might do something like this:

var table = adapter.GetData(); //get data from repository object -> DataTable

return table.Rows.Take(table.Rows.Count-1).Select(row => new HouseAnnouncement() {
    Area = float.Parse(row[table.powierzchniaColumn].ToString()),
    City = row[table.miastoColumn].ToString()
}).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文