0

In the below code block, my DataTable TelemetryFile is correctly populated. The first MessageBox displays 4.

It also correctly contains a column called FixName. The second MessageBox displays FixName.

However, something is going wrong when I try to create a List<(string FixOrReportName, long CountInTelemetryFile)> of the different FixName values in the table and how often they occur. sortedCountsByName is null and the third MessageBox diplays Hmm.

What am I doing wrong?


    public List<(string FixOrReportName, long CountInTelemetryFile)> GetMostPopularOptionsFromTelemetryFile(string typeToReturn, int maxOptionsToReturn)
    {
        this.TelemetryFile.DefaultView.RowFilter = "FixOrReport = '" + typeToReturn + "'";
        MessageBox.Show(this.TelemetryFile.DefaultView.Count.ToString());
        MessageBox.Show(this.TelemetryFile.Columns[3].ColumnName);

        var sortedCountsByName = (
            this.TelemetryFile.AsEnumerable()
           .GroupBy(x => x.Field<string>("FixName"))
           .Select(x => new Tuple<string, long>(x.Key, x.Count()))
        ) as List<(string FixOrReportName, long CountInTelemetryFile)>;

        if (sortedCountsByName is null)
        {
            MessageBox.Show("Hmm");
            return null;
        }
        else
        {
            sortedCountsByName.Sort(
                delegate (
                    (string FixOrReportName, long CountInTelemetryFile) firstPair,
                    (string FixOrReportName, long CountInTelemetryFile) nextPair)
                {
                    return -1 * firstPair.CountInTelemetryFile.CompareTo(nextPair.CountInTelemetryFile);
                }
            );
        }

        return sortedCountsByName.Take(maxOptionsToReturn).ToList();
    }
puzzlepiece87
  • 1,537
  • 2
  • 19
  • 36
  • 1
    You're mixing old `Tuple`s with new C#7 value tuples. Remove the `as` conversion and proceed from there. – Gert Arnold Nov 18 '19 at 19:50
  • @GertArnold Out of curiosity, is there a way for me to use new C#7 tuples inside the LINQ query? I tried several things but wasn't able to make it work. – puzzlepiece87 Nov 18 '19 at 19:53
  • @puzzlepiece87 Just do `Select(x => (x.Key, x.Count())` – juharr Nov 18 '19 at 19:54
  • @juharr I followed your suggestion, though with `Convert.ToInt64(x.Count())` to make the types match. Unfortunately, null was still returned. – puzzlepiece87 Nov 18 '19 at 20:26

2 Answers2

1
 .Select(x => new Tuple<string, long>(x.Key, x.Count()))

That's old Tuple syntax.

With C# 7 value tuples it should be:

.Select(x => (FixOrReportName: x.Key, CountInTelemetryFile: (long)x.Count())

Add ToList(), and you can remove the as conversion.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • This could very well be correct, but when I tried it before and now, it says: `new cannot be used with tuple type. Use a tuple literal expression instead. I'm using VS 2019 with a project targeting .Net Framework 4.7.2, so I believe I'm on C#7.3, if that helps. – puzzlepiece87 Nov 18 '19 at 20:36
  • Your edit solved the compile error, thank you. I'll run it again and accept if it works. – puzzlepiece87 Nov 18 '19 at 20:37
-3

The issue is with this as

.Select(x => new Tuple<string, long>(x.Key, x.Count()))
        ) as List<(string FixOrReportName, long CountInTelemetryFile)>;

Select() will return an IEnumerable<Tuple<string,int>> rather than a List<T>.

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
  • 1
    Are you saying you can't convert IEnumerable to List? Because you [can, though maybe I'm doing it incorrectly.](https://stackoverflow.com/questions/961375/casting-ienumerablet-to-listt) – puzzlepiece87 Nov 18 '19 at 20:19