0

I have a list that contains about 50 properties of a class. What I'm struggling with is I"m trying to take a count of the distinct values of two properties This "count" number then needs to be added on to the end of the list of each group by. For example: here is my class

 public class ListType
{
    public ListType()
    {
    }

    public string ID { get; set; }
    public string REGSID { get; set; }
    public string county { get; set; }
    public string act_loc { get; set; }
    public string eviofnd { get; set; }
    public string hmonth { get; set; }
    public string hisnc { get; set; }
    public string hinc { get; set; }
    public string FIPS { get; set; }
    public decimal back00 {get; set;}
    public decimal ann00 { get; set; }
    public int countDistinctID {get;set;}

  }

I need to take a count of distinct IDS of each distinct FIPS and then that count of the distinct IDS is set to countDistinctID property of each grouped FIPS. ID FIPS other columns(still need this data though)

  4        1      ......

  5        2       ....

  4        1      ....

  4        2       ....

  1        3     .....

  1        2      ....

  2        1      .....

What I need is this... ID FIPS other columns count 4 1 ...... 2 3 rows with FIPS =1 then 2 distinct ID's per FIPs=1

  5        2       ....         3   3 rows w/FIPS=2 3 distinct IDS per FIps=2    

  4        1      ....          2

  4        2       ....         3

  1        3     .....          1   1 row w/fips=3 1 distinct id perFIPS=3

  1        2      ....          3

  2        1      .....         2

I've parsed in a file and I write all the columns to a list called List data

what I ultimately need to do after I take count of distinct ID's is then set that count value to countDistinctID of the original "data" list and do that for each distinct FIPS and ID per that distinct FIPS census.countDistinctID

4

2 回答 2

2

尝试这个

var distinct = censu.Select(a => a.Select(x=>x.ID).Distinct().Count());
于 2013-10-20T22:09:19.073 回答
1
var censu = data.GroupBy(a => a.FIPS).ToList();
foreach (var grp in censu)
{
     var count = grp.Select(p=>p.ID).Distinct().Count();
     foreach (var d in grp)
             d.countDistinctID = count;
 }
于 2013-10-20T22:15:59.710 回答