Select records which the values of the field are the same

旧城冷巷雨未停 提交于 2021-01-29 09:26:08

问题


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?


回答1:


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);


来源:https://stackoverflow.com/questions/64150691/select-records-which-the-values-of-the-field-are-the-same

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!