marshall
marshall

Reputation: 74

each group by in list with linq

in List<file> i have data :

id   Initial         B
 1      G       (2016-27-12)
 2      H       (2016-27-15)
 3      G       (2016-27-16)

//my code
List<file> i = new List<file>;

var r = i.Select(i=> i.Initial).GroupBy(x => new { r = x.to List() });

for( int i = 0; i < r.Count(); i++ )
{
    comboBox1.Items.Add(r[i].ToString());
}

but my code still error.

how to GroupBy() with linq and each Initial result 2 count value G & H?

Upvotes: 0

Views: 62

Answers (2)

Benjamin Basmaci
Benjamin Basmaci

Reputation: 2567

I dont know if thats what you are trying to do but to me it looks like you want to get every unique Initial from your list. To accomplish that, you can use "Distinct":

var r = i.Select(i=> i.Initial).Distinct();

If this is not what you are trying to do, please provide more info.

Upvotes: 0

Eldaniz Ismayilov
Eldaniz Ismayilov

Reputation: 856

You can use

var r = i.Select(i=> i.Initial).GroupBy(x =>x).ToList();

Other way with Distinct()

 var r = i.Select(i=> i.Initial).Distinct().ToList();

Upvotes: 1

Related Questions