Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取数组在列表中出现的次数<&燃气轮机;_C#_Arrays_Linq - Fatal编程技术网

C# 获取数组在列表中出现的次数<&燃气轮机;

C# 获取数组在列表中出现的次数<&燃气轮机;,c#,arrays,linq,C#,Arrays,Linq,我正在为我的网站开发一个包含一些统计数据的部分,我很难弄清楚如何获得“流行的东西”(解释如下): 我有下表: ID | Spell1ID | Spell2ID 1 | 4 | 12 2 | 4 | 12 3 | 12 | 4 4 | 1 | 8 5 | 3 | 12 6 | 8 | 1 为了获得这些数据,我正在做以下工作: List<short[]> spellsList = new List<short[]>(); .. using (My

我正在为我的网站开发一个包含一些统计数据的部分,我很难弄清楚如何获得“流行的东西”(解释如下):

我有下表:

ID | Spell1ID | Spell2ID
1 | 4 | 12
2 | 4 | 12
3 | 12 | 4
4 | 1 | 8
5 | 3 | 12
6 | 8 | 1
为了获得这些数据,我正在做以下工作:

List<short[]> spellsList = new List<short[]>();

..

            using (MySqlDataReader dbReader = Conex.Command.ExecuteReader())
            {
                while (dbReader.Read())
                {
                    short[] tempArray = new short[2];

                    tempArray[0] = dbReader.GetInt16("spell1ID");
                    tempArray[1] = dbReader.GetInt16("spell2ID");

                    spellsList.Add(tempArray);
                }
            }
如果可以使用LINQ pref和lambda表达式实现这一点,那就太好了

很抱歉,如果这让人困惑,英语不是我的第一语言。

static void Main(string[]args)
    static void Main(string[] args)
    {
        var spellsList = new List<short[]>();

        spellsList.Add(new short[] {4, 12 });
        spellsList.Add(new short[] {4, 12 });
        spellsList.Add(new short[] {12, 4 });
        spellsList.Add(new short[] { 1, 8});
        spellsList.Add(new short[] {3, 12});
        spellsList.Add(new short[] {8, 1 });
        spellsList.Add(new short[] {8, 1 });
        spellsList.Add(new short[] {8, 1 });
        spellsList.Add(new short[] {8, 1 });


        var result = spellsList.Select(s => s[0] > s[1] ? String.Format("{0},{1}", s[0], s[1]) : String.Format("{0},{1}", s[1], s[0]))
                               .GroupBy(s => s)
                               .OrderByDescending(g => g.Count())
                               .ToList();

        result.ForEach(g => Console.WriteLine($"{g.Key}: {g.Count()} times"));

        Console.Read();
    }
{ var spellsList=新列表(); 添加(新的短[]{4,12}); 添加(新的短[]{4,12}); 添加(新的短[]{12,4}); 添加(新的短[]{1,8}); 添加(新的短[]{3,12}); 添加(新的短[]{8,1}); 添加(新的短[]{8,1}); 添加(新的短[]{8,1}); 添加(新的短[]{8,1}); var result=spellsList.Select(s=>s[0]>s[1]?String.Format(“{0},{1}”,s[0],s[1]):String.Format(“{0},{1}”,s[1],s[0])) .GroupBy(s=>s) .OrderByDescending(g=>g.Count()) .ToList(); ForEach(g=>Console.WriteLine($“{g.Key}:{g.Count()}次”); Console.Read(); }
您尝试过什么?堆栈溢出是为了帮助您编写自己的代码,而不是让其他人为您编写代码。请提供一份好的表格,清楚地显示您的尝试。也就是说,根据您目前的描述(我不太清楚),您似乎应该对
tempArray
进行排序,然后使用
IEqualityComparer
来处理数组比较。每个结果组的计数将告诉您每个组合出现的次数。@PeterDuniho我100%同意StackOverflow帮助编写我自己的代码,如果我的问题不清楚,我很抱歉。我没有尝试任何特别的东西,因为我在逻辑上被绊住了。现在,随着Mangist的回答,我意识到我应该如何使用LINQ来解决我的问题。感谢您的评论和您的时间。感谢您的回答,它的工作。现在我知道我必须更深入地研究LINQ,以了解将来如何解决这样的问题。非常感谢你的帮助。谢谢晓阳312的回答,你给了我另一种解决未来类似问题的方法。非常感谢!
    static void Main(string[] args)
    {
        var spellsList = new List<short[]>();

        spellsList.Add(new short[] {4, 12 });
        spellsList.Add(new short[] {4, 12 });
        spellsList.Add(new short[] {12, 4 });
        spellsList.Add(new short[] { 1, 8});
        spellsList.Add(new short[] {3, 12});
        spellsList.Add(new short[] {8, 1 });
        spellsList.Add(new short[] {8, 1 });
        spellsList.Add(new short[] {8, 1 });
        spellsList.Add(new short[] {8, 1 });


        var result = spellsList.Select(s => s[0] > s[1] ? String.Format("{0},{1}", s[0], s[1]) : String.Format("{0},{1}", s[1], s[0]))
                               .GroupBy(s => s)
                               .OrderByDescending(g => g.Count())
                               .ToList();

        result.ForEach(g => Console.WriteLine($"{g.Key}: {g.Count()} times"));

        Console.Read();
    }
// fake data
var data = @"1 | 4 | 12
            2 | 4 | 12
            3 | 12 | 4
            4 | 1 | 8
            5 | 3 | 12
            6 | 8 | 1"
.Split('\n')
.Select(x => x.Split(new[] { " | " }, StringSplitOptions.None));

var spellsList = data.Select(x => new[]
{
    int.Parse(x[1]), int.Parse(x[2])
});

// query
var combos = spellsList
    .GroupBy(spells => string.Join(", ", spells.OrderBy(x => x)), (k, g) => new
    {
        SpellCombo = k,
        CastCount = g.Count(),
    })
    .OrderBy(x => x.CastCount)
    .ToList();

foreach(var combo in combos)
{
    Console.WriteLine($"Combo {combo.SpellCombo} is casted {combo.CastCount} times. ");
}