统计可区分的列表视图项目的总数 [英] Count total of distinguishable listview items

查看:54
本文介绍了统计可区分的列表视图项目的总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个listview(Listview 1),我填写了数据。现在我想在第一个listview上收取第二个listview(listview 2)。这应该指示Collem [1] listview1并计算名称的出现次数。参见示例listview 2.



Listview 1

I have a listview (Listview 1) which I filled with data. Now I want to charge a second listview (listview 2) on the first listview. This should indicate Collem [1] listview1 and count the number of occurrences of the name. See Example listview 2.

Listview 1

ID *  NAAM  *  DATE
1 .     MO            01-01-2016
1 .     DE             01-01-2016
1 .     MO            02-01-2016
1 .     MO            02-01-2016
1 .     DE             02-01-2016
1 .     MO            03-01-2016
1 .     MO            02-01-2016





Listview 2



Listview 2

NAAM  *  Total count
MO    (TOTAL 5)         
DE     (TOTAL 3)





我尝试过:



我发现了这个;但是这个脚本在名称上没有区别。





What I have tried:

I found this; but this script makes no distinction in names..

var query = listView.Items
                    .Cast<listviewitem>()
                    .Where(item => item.SubItems[3].Text == "asdf");
foreach (var item in query)
{
    ...
}

推荐答案

MaikelO1 [ ^ ]在评论中写道:
MaikelO1[^] wrote in comment:

private void Registratiefilter()
        {
            ListBoxUren.TabIndex = 0;
            ListBoxUren.Items.Clear();
            ListBoxUren.View = View.Details;

            ListBoxUren.MultiSelect = false;
            ListBoxUren.FullRowSelect = true;

            using (var connection = new SqlConnection(connectionstring))
            {

                var query = "SELECT ID, Start, Stop, Pauze, SoortOmschrijving, Naam, IDProjecten, Organisatie, AardOmschrijving, ExtraOmschrijving, (Projectnummer + ' - ' + Organisatie +  ' - ' + Referentie) AS REFProject, (MaterieelNR + ' - ' + Materieelstuk) AS REFMaterieel  FROM UrenREG LEFT OUTER JOIN Medewerkers ON UrenReg.FK_IDMedewerker = Medewerkers.IDMedewerker LEFT OUTER JOIN UrenRegSoortActiviteit ON UrenReg.FK_SoortActiviteitID = UrenRegSoortActiviteit.SoortID LEFT OUTER JOIN Projecten ON UrenReg.FK_ProjectID = Projecten.IDProjecten LEFT OUTER JOIN Klanten ON Projecten.FK_IDKlant = Klanten.IDKlanten LEFT OUTER JOIN UrenRegAardVanActiviteit ON UrenReg.Fk_AardVanActiviteitID = UrenRegAardVanActiviteit.AardVanActiviteitID LEFT OUTER JOIN Materieel ON UrenReg.FK_materiaalID = Materieel.IDMaterieel LEFT OUTER JOIN UrenRegExtra ON UrenReg.FK_ExtraID = UrenRegExtra.ExtraID WHERE Start like @Date";
                
                using (var adapter = new SqlDataAdapter(query, connection))
                {

                    SqlParameter parm1 = adapter.SelectCommand.Parameters.AddWithValue("@Date", "%" + LB_DatePick.Text + "%");

                    try
                    {
                        DataTable ProjectLijst = new DataTable();
                        adapter.Fill(ProjectLijst);

                        for (int i = 0; i < ProjectLijst.Rows.Count; i++)
                        {
                            DataRow dr = ProjectLijst.Rows[i];
                            ListViewItem listitem = new ListViewItem(dr["Naam"].ToString());
                            listitem.SubItems.Add(dr["Start"].ToString());
                            listitem.SubItems.Add(dr["Stop"].ToString());
                            listitem.SubItems.Add(dr["Pauze"].ToString());
                            listitem.SubItems.Add(dr["AardOmschrijving"].ToString());
                            listitem.SubItems.Add(dr["SoortOmschrijving"].ToString());
                            listitem.SubItems.Add(dr["REFProject"].ToString() + dr["REFMaterieel"].ToString());
                            listitem.SubItems.Add(dr["ExtraOmschrijving"].ToString());

                            listitem.Tag = dr["ID"];
                            ListBoxUren.Items.Add(listitem);

                            ListBoxUren.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                            ListBoxUren.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
                        }
                    }
                    catch (Exception)
                    {





首先,第二个,最后: 你应该处理数据,而不是控件!



关于如何获得一些数据的明显错误:

你有2种方法可以达到这个目的:

1)您可以询问您的数据库并将结果返回到第二个ListView

2)您可以通过使用Linq



广告1)



First of all, second of all and finally: You should work on data, not on controls!

As to your confusion about how to get distinct count of some data:
You've got 2 ways to achieve that:
1) you can "ask" your database and return result to the second ListView
2) you can get the count of distinct values by grouping data from the datatable via using Linq

Ad 1)

SELECT Name, COUNT(DISTINCT Name)
FROM YourTable
GROUP BY Name





Ad 2)



Ad 2)

var result = ProjectLijs.AsEnumerable()
    .GroupBy(x => x.Field<string>("Name"))
    .Select(grp => new
          {
              Name = grp.Key,
              Count = grp.Distinct().Count()
          })
     .ToList()





所有你现在要做的事情,是将数据绑定到第二个ListView。



尝试你喜欢的方法!请注意,Linq解决大数据量可能会导致效率问题。



有关详细信息,请参阅:

LINQ to DataSet [ ^ ]

LINQ to DataSet示例 [ ^ ]

LINQ to DataSet中的查询 [ ^ ]

查询数据集(LINQ to DataSet) [ ^ ]

101 C#中的LINQ示例 [ ^ ]





示例:





All what you have to do right now, is to bind that data to the second ListView.

Try the method you prefer! Note, that Linq solution for large data amount may cause efficiency issues.

For further details, please see:
LINQ to DataSet[^]
LINQ to DataSet Examples[^]
Queries in LINQ to DataSet[^]
Querying DataSets (LINQ to DataSet)[^]
101 LINQ Samples in C#[^]


Example:

DataTable dt = new DataTable();

dt.Columns.Add(new DataColumn("ID", typeof(int)));
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Date", typeof(DateTime)));

dt.Rows.Add(new object[]{1, "MO", new DateTime(2016,1,1)});
dt.Rows.Add(new object[]{1, "DE", new DateTime(2016,1,1)});
dt.Rows.Add(new object[]{1, "MO", new DateTime(2016,1,2)});
dt.Rows.Add(new object[]{1, "MO", new DateTime(2016,1,2)});
dt.Rows.Add(new object[]{1, "DE", new DateTime(2016,1,2)});
dt.Rows.Add(new object[]{1, "MO", new DateTime(2016,1,3)});
dt.Rows.Add(new object[]{1, "MO", new DateTime(2016,1,2)});

var result = dt.AsEnumerable()
		.GroupBy(x=>x.Field<string>("Name"))
		.Select(grp=>new
			{
				Name = grp.Key,
				Count = grp.Distinct().Count()
			});





以上代码返回:



Above code returns:

MO 5 
DE 2 





[/ EDIT]



[/EDIT]


参考这个例子



refer this example

private void Form3_Load(object sender, EventArgs e)
       {
           listView1.View = View.Details;
           listView1.GridLines = true;


           ListViewItem item1 = new ListViewItem("1");
           item1.SubItems.Add("MO");
           item1.SubItems.Add("01-01-2016");
           ListViewItem item2 = new ListViewItem("1");
           item2.SubItems.Add("DE");
           item2.SubItems.Add("01-01-2016");
           ListViewItem item3 = new ListViewItem("1");
           item3.SubItems.Add("MO");
           item3.SubItems.Add("01-01-2016");
           ListViewItem item4 = new ListViewItem("1");
           item4.SubItems.Add("MO");
           item4.SubItems.Add("01-01-2016");
           ListViewItem item5 = new ListViewItem("1");
           item5.SubItems.Add("DE");
           item5.SubItems.Add("01-01-2016");
           ListViewItem item6 = new ListViewItem("1");
           item6.SubItems.Add("MO");
           item6.SubItems.Add("01-01-2016");
           ListViewItem item7 = new ListViewItem("1");
           item7.SubItems.Add("MO");
           item7.SubItems.Add("01-01-2016");


           listView1.Columns.Add("ID"  );
           listView1.Columns.Add("NAAM");
           listView1.Columns.Add("DATE" );

           listView1.Items.AddRange(new ListViewItem[] { item1, item2, item3, item4,item5,item6,item7 });

           int columnToCheckIndex = 1; // set the index value of the column to be checked
          List<string> lst = new List<string> ();
           foreach (ListViewItem item in listView1.Items)
               lst.Add(item.SubItems[columnToCheckIndex].Text);
           var data = lst.GroupBy(k => k).Select(k => new { key = k.Key, count = k.Count() }).ToList();
           foreach (var item in data)
           {
               ListViewItem lvi = new ListViewItem(item.key);
               lvi.SubItems.Add(item.count.ToString());
               listView2.Items.Add(lvi);

           }

           listView2.Columns.Add("NAAM");
           listView2.Columns.Add("Total Count");
           listView2.View = View.Details;
           listView2.GridLines = true;





       }


请采取此解决方案不要太认真,我对这个Linq的东西一无所知。我也不知道这是不是正确的方法。但如果我看一下结果,至少对我来说它解决了这个问题。

Please take this solution not too serious, I have no clue about this Linq stuff. I have also no idea whether this is the correct way. But if I have a look to the result, for me at least it solves the request.
private void buttonListViewLinq_Click(object sender, EventArgs e)
{
    // Test Source ListView
    listViewLinq.Items.Clear();
    listViewLinq.Items.Add(new ListViewItem (new string[]{"1", "A", "2016-08-01"}));
    listViewLinq.Items.Add(new ListViewItem (new string[]{"1", "A", "2016-08-02"}));
    listViewLinq.Items.Add(new ListViewItem (new string[]{"1", "B", "2016-08-03"}));
    listViewLinq.Items.Add(new ListViewItem (new string[]{"1", "B", "2016-08-04"}));
    listViewLinq.Items.Add(new ListViewItem (new string[]{"1", "B", "2016-08-05"}));
    listViewLinq.Items.Add(new ListViewItem (new string[]{"2", "B", "2016-08-06"}));
    listViewLinq.Items.Add(new ListViewItem (new string[]{"1", "C", "2016-08-07"}));

    // Get IEnumerable to go on with linq
    IEnumerable<ListViewItem> lv = listViewLinq.Items.Cast<ListViewItem>();

    // Here grouping happens
    var res = lv.GroupBy(gb => gb.SubItems[1].Text)
                .Select(group => new { groupVal = group.Key, 
                                       groupCount = group.Count() });

    // Columns of Result ListView
    listViewRes.Columns.Clear();
    listViewRes.Columns.Add("NAAM");
    listViewRes.Columns.Add("Total Count", 100);
    listViewRes.View = View.Details;

    // Add Result ListViewItems
    listViewRes.Items.AddRange(res.Select(g => new ListViewItem(new string[]
         {g.groupVal, 
          String.Format("(TOTAL {0})", g.groupCount.ToString())
         })).ToArray());
}





注意:根据您的示例结果,您在Count中感兴趣并且_not_在Distinct Count中。

我希望它有所帮助。



Note: According your example result you are interesting in Count and _not_ in Distinct Count.
I hope it helps.


这篇关于统计可区分的列表视图项目的总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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