Habip Oğuz
Habip Oğuz

Reputation: 1193

Select records which the values of the field are the same

enter image description here

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

var decisions = _db.decisions.Where(x => x.folder_id == Folder.Id).ToList();

If the values of the 'rec_no' field in this list are the same, I would like to output these records in a foreach loop and remaining records to a separate foreach loop. But I have no idea about it.

EDIT 1: I want to output 'rec_no' is 13 in a foreach loop and others are in seperated foreach loop which 'folder_id' is 87.

EDIT 2: In the example below, is it possible to output 'rec_no' is 13 ones a loop, 'rec_no' is 5 ones a loop, and the others a loop?

enter image description here

Upvotes: 0

Views: 84

Answers (1)

Derviş Kayımbaşıoğlu
Derviş Kayımbaşıoğlu

Reputation: 30565

I guess you are looking for something like this

var decisions = _db.decisions.Where(x => x.folder_id == Folder.Id).ToList();

var dupKeys = decisions.GroupBy(x => x.RecNo)
              .Where(g => g.Count() > 1)
              .Select(y => y.Key)
              .ToList();

var dupList = decistions.Where(x => dupKeys.Contains(x.RecNo)).ToList();

var restDecisions = decisions.Except(dupList);

Upvotes: 2

Related Questions