输出记录,该字段的值相同 [英] Output records which the values of the field are the same

查看:50
本文介绍了输出记录,该字段的值相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该主题可能被复制了.我在本主题下的另一种情况下问过,并由DervişKayımbaşıoğlu回答.当我这样编辑主题并再次提问时,DervişKayımbaşıoğlu说我应该在一个新主题中提出这个问题.所以我不得不在一个新的话题中提出这个问题.

这是数据模式示例:

我有一个像这样从SQLite数据库获取的列表:

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

如果此列表中REC_ID字段的值相同,我想将这些记录输出到foreach循环中,并将其余记录输出到单独的foreach循环中.例如,在上面的列表中,REC_ID 13个在循环中,REC_ID 5个在循环中,REC_ID 7个在循环中,其他在最后一个循环中.

从评论中

我想将具有多个相同值的余数分开,在我们的示例中,应将ID的{1, 2, 3} {4, 5} {6, 7}{8, 9}分开.

解决方案

我想将具有多个相同值的余数分开,在我们的示例中,应将ID的{1, 2, 3} {4, 5} {6, 7}{8, 9}分开.

好,那么您可以按REC_ID进行分组,并检查该组的Count()是否大于1(给您多个相同值")或等于1(给您余数") ;),后者必须再次展平才能将它们重新放回同一收藏集中

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

这是一个示例程序,其输出显示如下:

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

}

输出:

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

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:

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.

解决方案

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

这篇关于输出记录,该字段的值相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆