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