简体   繁体   English

显示数据库中的项目(C#)

[英]Displaying items from database (c#)

I'm tallying a row in my access database. 我在访问数据库中记录了一行。 My databases contain movie company,movies produced by companies and the amount of movies sold. 我的数据库包含电影公司,公司制作的电影和售出的电影数量。 There is more info in the database than I mentioned, but for this problem I'm only looking at two columns. 数据库中的信息比我提到的要多,但是对于这个问题,我只看两列。 The movie company and the movies produced by that company. 电影公司和该公司制作的电影。 One column is called company and the other is called Movie. 一列称为公司,另一列称为电影。 I need to count every movie that was produced by the movie company. 我需要计算电影公司制作的每部电影。 I counted six different Movie companies in the database. 我在数据库中算出了六个不同的电影公司。 My goal is to simply display the movie company and the number movies it produced in a listbox. 我的目标是仅在列表框中显示电影公司及其制作的电影编号。

I have two big problems. 我有两个大问题。 1. My result won't display in the listbox. 1.我的结果不会显示在列表框中。 2 According to the good people who looked at my code. 2据看我代码的好人说。 The program is not counting anything. 该程序不计任何东西。 Here is example of what I want to display 这是我要显示的示例

Company #Movies 公司#电影

Paramount 4 最重要的4

20thCenturyfox 8 20世纪狐狸8

LucasFilm 6 卢卡斯电影6

etc. 等等

What I'm thinking is created a nested foreach loop and for every movie that is produced by a certain company add it to the listbox. 我在想的是创建一个嵌套的foreach循环,并将某家公司生产的每部电影都添加到列表框中。

private void tabP3_Click(object sender, EventArgs e)
    {
       foreach (DataRowView row in bindingSource1.List)
        {
            foreach (DataRowView row2 in bindingSource1.List)
            {
              if(((String)row2["Company"]).Equals(row["Movie"]))
                {
                int count = 0;
                count++;
                lbCompany.Items.Add((String.Format("{0,5}", count)));
                }

            }

        }

    }

Why don't you use Linq to achieve the solution? 您为什么不使用Linq来实现解决方案? That's what I would do, in the first place, to tackle your requirement. 首先,这就是我要做的,以满足您的要求。

The following is taken from this post . 以下摘自这篇文章 "data" should be your collection... here you will get a new type that contains 2 fields (which you can edit/change per your convenience). “数据”应该是您的集合...在这里,您将获得一个包含2个字段的新类型(您可以根据需要方便地进行编辑/更改)。 You may also want to remove the OrderBy. 您可能还需要删除OrderBy。

foreach(var line in data.GroupBy(info => info.metric)
                        .Select(group => new { 
                             Metric = group.Key, 
                             Count = group.Count() 
                        })
                        .OrderBy(x => x.Metric)
{
     Console.WriteLine("{0} {1}", line.Metric, line.Count);
}

First it is most certain you never entering the if statement since you are comparing for equality to different objects. 首先,可以肯定的是您从未输入过if语句,因为您要比较不同对象的相等性。 One object of datatype string: (String)row2["Company"] and one object of datatype System.Data.DataRow row["Movie"] . 数据类型字符串的一个对象: (String)row2["Company"]和数据类型System.Data.DataRow row["Movie"]一个对象。 Further in each iteration you set counter back to zero. 此外,在每次迭代中,您都将计数器设置回零。 So try: 因此,请尝试:

 int count = 0;
 foreach (DataRowView row in bindingSource1.List)
        {
            foreach (DataRowView row2 in bindingSource1.List)
            {
              if((row2["Company"].ToString()).Equals(row["Movie"].ToString()))
                {               
                count++;
                lbCompany.Items.Add((String.Format("{0}", count)));
                }

            }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM