简体   繁体   English

Linq独特计数

[英]Linq distinct count

I have some data returned as the follow: 我返回了一些数据,如下所示:

Model { Guid Id }

and I have an int of the total items, int totalCount . 我有一个总项目的int totalCountint totalCount

I am trying to get the distinct count from the data that were returned as Category 1, and have the total item minus the count from Category 1 to be Category 2. 我正在尝试从作为类别1返回的数据中获得不同的计数,并使总项目减去类别1中的计数成为类别2。

I have the following linq to get the distinct count for Category 1, but how do I add the count for Category 2 into the IEnumerable? 我有以下linq来获取类别1的非重复计数,但是如何将类别2的计数添加到IEnumerable中?

var result = data.
    GroupBy(x => x.Id).
    Select(x => new { Category = "1", Value = x.Select(v => v.Id).Distinct().Count() }).
    GroupBy(x => x.Category).
    Select(x => new Item { Category = x.Key, Value = x.Sum(y => y.Value) });

and the Item Class have 2 members: string for Category and decimal for Value. 项目类有2个成员:字符串表示类别,十进制表示值。

Thanks! 谢谢!


Edit: So I have the follow data IEnumerable<Model> data , and it contains the following data: 编辑:所以我有以下数据IEnumerable<Model> data ,它包含以下数据:

{ Id: 1 }
{ Id: 2 }
{ Id: 2 }
{ Id: 1 }
{ Id: 3 }
{ Id: 4 }

and totalCount = 10. 和totalCount = 10。

I would like to get the distinct count of Id from data, which is 4 in this case, as category 1, and totalCount - distinct item to be category 2. So for the result I will have a IEnumerable of Item to have the following: 我想从数据中获得Id的非重复计数,在这种情况下为4,作为类别1,totalCount-非重复项成为类别2。因此,对于结果,我将获得IEnumerable of Item使其具有以下内容:

{ Category: "1", Value: 4 }, { Category: "2", Value: 6 }

For now my linq statement only return { Category: "1", Value: 4 } , and I have no idea what to do next. 现在,我的linq语句仅返回{ Category: "1", Value: 4 } ,我不知道下一步该怎么做。

Your question is not very clear, but I think this is what you want: 您的问题不是很清楚,但是我想这就是您想要的:

int totalCount = data.Count();
int distinctCount = data.Select(x => x.Id).Distinct().Count();
List<Item> result = new List<Item>
{
    new Item() { Category = "1", Value = distinctCount },
    new Item() { Category = "2", Value = totalCount - distinctCount },
};

Do you need two objects at the end of the day, or can it just be part of one return? 您一天结束时需要两个物品,还是可以作为一次退货的一部分? You could just do: 您可以这样做:

var result = data.
    GroupBy(x => x.Id).
    Select(x => new { 
       DistinctCount = x.Select(v => v.Id).Distinct().Count(),
       TotalCount = data.Count()
    });

This or something similar would probably work. 这或类似的东西可能会起作用。

I use the data structure to face the situation. 我使用数据结构来面对这种情况。

Dictionary< string, List< YOUR_TYPE >> dictionary = new Dictionary<string, List< YOUR_TYPE >> ();

We can get different keys from source. 我们可以从源代码获得不同的密钥。

var keys = YOUR_DATA_SOURCE.Select(item => item.YOUR_KEY_NAME ).Distinct();

Just like SQL : "SELECT DISTINCT YOUR_KEY_NAME FROM YOUR_DATA_SOURCE". 就像SQL一样:“从YOUR_DATA_SOURCE中选择DISTINCT YOUR_KEY_NAME”。

foreach (var key in keys) {
    var list = new List< YOUR_TYPE > ();
    list.AddRange (dataSource.Where (x => x.YOUR_Filter_Property == YOUR_KEY_NAME ).ToList ());

    dictionary.Add (key, list);
}

Then we can use Dictionary's default features, like "ContainKey", "Keys" to help us. 然后,我们可以使用Dictionary的默认功能(例如“ ContainKey”,“ Keys”)来帮助我们。

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

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