Habip Oğuz
Habip Oğuz

Reputation: 1193

Output records which the values of the field are the same

This topic is may be dublicated. I asked in a different scenario under this topic and it is answered by Derviş Kayımbaşıoğlu. When I edited my topic like this and asked again, Derviş Kayımbaşıoğlu said I should ask this in a new topic. So I had to ask the question in a new topic.

Here is data schema example:

enter image description here

I have a list getting from the SQLite database like this:

var decisions = _db.decisions.Where(x => x.CAT_ID == Cat.Id).ToList();

If the values of the REC_ID field in this list are the same, I would like to output these records in foreach loops and remaining records to a separate foreach loop. For example in the list above, REC_ID 13 ones are a in a loop, REC_ID 5 ones are a in a loop, REC_ID 7 ones are a in a loop and the others are in a last loop.

Edit from comment:

I want to separate those with more than one same value and the remainder.In our example, IDs' {1, 2, 3} {4, 5} {6, 7} and {8, 9} should be seperated.

Upvotes: 3

Views: 59

Answers (1)

Mong Zhu
Mong Zhu

Reputation: 23732

I want to separate those with more than one same value and the remainder.In our example, IDs' {1, 2, 3} {4, 5} {6, 7} and {8, 9} should be seperated.

Ok then you can group by the REC_ID and check wether the Count() of the group is larger than 1 (giving you "more than one same value") or equal to 1 (giving you "the remainder"), the latter has to be flattened again to get them into the same collection back together

List<IGrouping<int, MyClass>> recIdGroupsMultipleEntries = decisions.GroupBy(x => x.REC_ID)
                                                               .Where(g => g.Count() > 1).ToList();
List<MyClass> recIdSingleEntries = list.GroupBy(x => x.REC_ID)
                                             .Where(g => g.Count() == 1)
                                             .SelectMany(flat => flat).ToList();

Here is an examplary programm with output to show this:

void Main()
{
    List<MyClass> list = new List<MyClass>();
    for (int i = 1; i < 10; i++)
    {   
        list.Add(new MyClass { ID = i, TITLE = $"title {i}", CAT_ID = 81});
    }

    list[0].REC_ID = 13;
    list[1].REC_ID = 13;
    list[2].REC_ID = 13;
    list[3].REC_ID = 5;
    list[4].REC_ID = 5;
    list[5].REC_ID = 7;
    list[6].REC_ID = 7;
    list[7].REC_ID = 1;
    list[8].REC_ID = 2;


    List<IGrouping<int, MyClass>> recIdGroupsMultipleEntries = list.GroupBy(x => x.REC_ID).Where(g => g.Count() > 1).ToList();
    List<MyClass> recIdSingleEntries = list.GroupBy(x => x.REC_ID)
                                                                 .Where(g => g.Count() == 1)
                                                                 .SelectMany(flat => flat).ToList();
    
    foreach (var group in recIdGroupsMultipleEntries)
    {
        Console.WriteLine($"REC_ID: {group.Key} || {string.Join(", ", group.Select(x => x.ID))}");  
    }

    Console.WriteLine($"REC_ID: {recIdSingleEntries.Select(x => x.REC_ID).First()} || {string.Join(", ", recIdSingleEntries.Select(x => x.ID))}");

}

Output:

REC_ID: 13 || 1, 2, 3
REC_ID: 5 || 4, 5
REC_ID: 7 || 6, 7
REC_ID: 1 || 8, 9

Upvotes: 1

Related Questions