简体   繁体   English

计算ObservableCollection中等于1的项目数-C#

[英]Counting number of items in ObservableCollection where it equals 1 - C#

I made an SQL query and filled the data to an ObservableCollection. 我进行了SQL查询,并将数据填充到ObservableCollection中。 The database contains many columns so I want to count how many instances where a specific column = 1, then return that number to an int. 该数据库包含许多列,因此我想计算一个特定列= 1的实例数,然后将该数字返回给int。

The query: 查询:

var test = from x in m_dcSQL_Connection.Testheaders 
           where dtStartTime <= x.StartTime && dtEndtime >= x.StartTime 
           select new {
                         x.N, 
                         x.StartTime, 
                         x.TestTime, 
                         x.TestStatus, 
                         x.Operator, 
                         x.Login,
                         x.DUT_id, 
                         x.Tester_id, 
                         x.PrintID 
                       };

Then I add the data pulled from the database to an Observable Collection via: 然后,我通过以下方式将从数据库中提取的数据添加到Observable集合中:

lstTestData.Add(new clsTestNrData(item.N.ToString(), 
                    item.StartTime.ToString(), 
                    item.TestTime.ToString()
                    etc.....

I want to count how many times TestStatus = 1. 我想计算TestStatus = 1的次数。

I have read about the .Count property but I do not fully understand how it works on ObservableCollections. 我已经读过有关.Count属性的信息,但我还不完全了解它如何在ObservableCollections上工作。

Any help? 有什么帮助吗?

The standard ObservableCollection<T>.Count property will give you the number of items in the collection. 标准的ObservableCollection<T>.Count属性将为您提供集合中的项目数。

What you are looking for is this: 您正在寻找的是:

testStatusOneItemCount = lstTestData.Where(item => item.TestStatus == 1).Count()

...which uses IEnumerable<T>.Count() method which is part of LINQ. ...使用LINQ的IEnumerable<T>.Count()方法。

To elaborate a bit, Count will simply count the objects in your collection. 为了详细说明, Count将仅对集合中的对象进行计数。
I suggest having a quick look at linq 101 . 我建议快速看一下linq 101 Very good examples. 很好的例子。

Here's an example: 这是一个例子:

// Assuming you have :
var list = new List<int>{1,2,3,4,5,6 };
var items_in_list = list.Count(); //  = 6;

Using linq's Where , you're basically filtering out items, creating a new list. 使用linq的Where ,您基本上是在过滤项目,创建一个新列表。 So, the following will give you the count of all the numbers which are pair: 因此,以下将为您提供所有成对数字的计数:

var pair = list.Where(item => item%2 ==0);
var pair_count = pair.Count; // = 3

You can combine this without the temp variables: 您可以不使用temp变量将其组合:

var total = Enumerable.Range(1,6).Where(x => x % 2 ==0).Count(); // total = 6;

Or you can then select something else: 或者您可以选择其他内容:

var squares_of_pairs = Enumerable.Range(1,6)
                                .Where(x => x % 2 ==0).
                                .Select( pair => pair*pair);
// squares_of_pairs = {4,16, 36}. You can count them, but still get 3 :)

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

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