查找具有特定值的项目,移动并删除它。 C#,LINQ

时间:2018-03-14 20:34:18

标签: c# linq

这个程序应该生成数字,然后选择唯一的数字并计算这个数字在生成过程中出现的次数 - 它现在正在运作。

现在我需要选择eg.5具有最高item.value的项目并将其显示为:

名称(仅限唯一编号)=值(外观数量)

5 = 40

6 = 70 ...

现在我只找到具有最高价值的物品。 任何提示如何选择我找到的具有最高值的项目,将其移至另一个列表,从原始列表中删除它,然后显示所有具有最高值的项目?

这是我第一篇文章,因为我刚开始学习编程,所以很抱歉出错。

using System;
using System.Collections.Generic;
using System.Linq;

namespace Lotto_2._0
{
    class Program
    {
        static void Main()
        {
            var generated = new List<int>();
            var random = new Random();
            var uniques = new List<int>();
            var itemList = new List<Item>();
            var maxValueList = new List<int>();

            // Random numbers generating (from 1 to 49)
            for (int i = 0; i < 2400; i++)
            {
                var number = random.Next(1, 50);
                generated.Add(number);
            }

            // Unique numbers selecting
            foreach (var number in generated)
            {
                if (!uniques.Contains(number)) uniques.Add(number);
            }

            Console.WriteLine("Number of generated items: " + generated.Count);
            Console.WriteLine("Unique items number: " + uniques.Count);

            // Unique number repeating count
            foreach (var unique in uniques)
            {
                var countedUniques = 0;
                foreach (var number in generated)
                {
                    if (unique == number)
                        countedUniques ++;
                }
                // Save to itemList
                var item = new Item(unique,countedUniques);
                itemList.Add(item);
            }

            var editedItemList = itemList;
            foreach (var item in itemList)
                Console.WriteLine($"{item.Name} = {item.Value}");

            // Searching max value item
            var maxValue = editedItemList.Max( x => x.Value);
            maxValueList.Add(maxValue);
            Console.WriteLine(maxValue);
        }
    }

    public class Item
    {
        public int Name { get; protected set; }
        public int Value { get; protected set; }

        public Item(int name, int value)
        {
            Name = name;
            Value = value;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

LINQ有许多操作只能使用一个集合来帮助解决这个问题。以下示例以List<int>生成的值开头,计算每个唯一编号出现的次数,然后获取具有最多外观的5个值。请注意,原始列表实际上不受影响。按照设计,LINQ不会修改原始集合,而是返回可以枚举的新集合。

using System;
using System.Collections.Generic;
using System.Linq;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            var generated = GenerateList();

            var uniqueItems = generated
                // Groups each number by the given key (itself). Each group will contain one distinct number
                .GroupBy(x => x)
                // Creates key/value pairs. The key is the unique number. The value is the number of appearances
                .ToDictionary(group => group.Key, group => group.Count());

            Console.WriteLine("Number of generated items: " + generated.Count);
            Console.WriteLine("Unique items number: " + uniqueItems.Count);
            foreach (var item in uniqueItems)
            {
                Console.WriteLine($"{item.Key} = {item.Value}");
            }

            var highestFive = uniqueItems
                // Sort the items by their appearances (highest first)
                .OrderByDescending(item => item.Value)
                // Only keep the first 5 items
                .Take(5);

            Console.WriteLine("Numbers with the highest 5 occurrences");
            foreach (var item in highestFive)
            {
                Console.WriteLine(item.Key);
            }

            /* Output:
             * Number of generated items: 20
             * Unique items number: 6
             * 10 = 2
             * 25 = 3
             * 30 = 5
             * 45 = 4
             * 5 = 5
             * 15 = 1
             * Numbers with the highest 5 occurrences
             * 30
             * 5
             * 45
             * 25
             * 10
             */
        }

        private static List<int> GenerateList()
        {
            // For simplicity, using known variables
            // This function could generate any type of list you want
            return new List<int>
            {
                10, 25, 30, 45, 30, 45, 25, 30, 25, 30, 5, 5, 15, 30, 45, 10, 45, 5, 5, 5
            };
        }
    }
}

另一种方法是计算使用Dictionary<int, int>生成的每个数字的频率,如Eric Lippert已经建议的那样。以下示例使用Dictionary,因此不需要执行上一示例的GroupBy操作。

using System;
using System.Collections.Generic;
using System.Linq;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            var generated = 0;
            var random = new Random();
            var items = new Dictionary<int, int>();

            for (int i = 0; i < 2400; i++)
            {
                var number = random.Next(1, 50);
                generated++; // Keep track of how many have been generated

                if (items.ContainsKey(number))
                {
                    // If the number is not unique, increase its appearances by 1
                    items[number]++;
                }
                else
                {
                    // If the number is unique, set its appearances to 1
                    items.Add(number, 1);
                }
            }

            Console.WriteLine("Number of generated items: " + generated);
            Console.WriteLine("Unique items number: " + items.Count);
            foreach (var item in items)
            {
                Console.WriteLine($"{item.Key} = {item.Value}");
            }

            var highestFive = items
                // Sort the items by their appearances (highest first)
                .OrderByDescending(item => item.Value)
                // Only keep the first 5 items
                .Take(5);

            Console.WriteLine("Numbers with the highest 5 occurrences");
            foreach (var item in highestFive)
            {
                Console.WriteLine(item.Key);
            }
        }
    }
}