Newby) filter rows from csv using linq

Hi,

My final goal is to use data from a csv-file to instantiate filtered objects in Unity (with c#). The first step is to couple a csv in Unity.

Linq is a very interesting way to achieve this. Next to csv, it can also be used to harvest other data sources.

I found an interesting example on the website of Microsoft:

My csv file looks like this:

To use it in Unity I converted the example:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
using System;

public class Linqtest : MonoBehaviour
    
{
    public string output = "";
    public float A = 35f, B = 150f, C = 750f, D = 400f;

    public Text teksten;

    public string[] line;

    void Start()
   
    {
   
        string[] lines = System.IO.File.ReadAllLines("./Assets/LINQ/Scores.csv");

        MultiColumns(lines);

    }
    public void MultiColumns(IEnumerable<string> strs)
    {
       
        IEnumerable<IEnumerable<int>> multiColQuery =

            
            from line in strs
            let elements = line.Split(',')
            let scores = elements.Skip(1)
            select (from str in scores
                    select Convert.ToInt32(str));

    var results = multiColQuery.ToList();

            // Loop en berekenen Queries:

        int columnCount = results[0].Count();
       
        output = "";

        for (int column = 0; column < columnCount; column++)
        {
            var results2 = from row in results
                           select row.ElementAt(column);
   
            double average = results2.Average();
            int max        = results2.Max();
            int min        = results2.Min();
            int sum        = results2.Sum();

            output = output + (string.Format("Examennummer #{0} Gemiddelde: {1:##.##} Beste Score: {2} Laagste Score: {3} Opgeteld: {4}",
                          column + 1, average, max, min, sum));
            output = output + "\n";
        }
    }
    void OnGUI()
    {
        GUI.Label(new Rect(A, B, C, D), output);
    }
}

Like you can see, the results are columns. I’ve spend some evenings to convert the code that it shows rows. Unfortunately without a good result.

Can someone advise me how I can show the rows filtered by the first column?
For example, show the data from rows called A.

Thanks in advance,

Louis

You can do it like this:

var fiteredResults = System.IO.File.ReadAllLines("./Assets/LINQ/Scores.csv")
    .Select(l=>l.Split(',').Select(s=>s.Trim()).ToArray())
    .Where(a=>a[0] == "A");

foreach (var filteredResult in fiteredResults)
{
    Debug.Log(String.Join(",", filteredResult));
}

or if you prefer:

var fiteredResults =
 from a in (from l in System.IO.File.ReadAllLines("./Assets/LINQ/Scores.csv") select (from s in l.Split(',') select s.Trim()).ToArray()) where  a[0] == "A" select a;

foreach (var filteredResult in fiteredResults)
{
    Debug.Log(String.Join(",", filteredResult));
}

Thank you very much for the answer Fido!

I hope to test it today.

Regards Louis

tested?

Also slow, and not very garbage allocation friendly.

Although this is a necropost (kinda), people should know that this is far from optimal.