Dr.Bake
Dr.Bake

Reputation: 151

Call LINQ Query within If Condition in C#/WPF

This question is a continuation of the question I asked here: Group by column to count duplicated values from another column in LINQ WPF

Thanks to the great help of the people here, I was able to obtain the below output

+--------+-------+---------+------+
|  Name  | Score | Average | Star |
+--------+-------+---------+------+
| John   |    15 |       5 |    3 |
| Hannah |    13 |     4.3 |    1 |
| Lilly  |    13 |     4.3 |    2 |
+--------+-------+---------+------+

So I was wondering if it's possible to get the below output as well

+--------+-------+---------+------+------+
|  Name  | Score | Average | Star |Letter|
+--------+-------+---------+------+------+
| John   |    15 |       5 |    3 |    A |
| Hannah |    13 |     4.3 |    1 |    C |
| Lilly  |    13 |     4.3 |    2 |    B |
+--------+-------+---------+------+------+

Basically, for 3 stars, the letter should be A, for 2, B and for 1, C. I figured this can be easily doable using an if statement, but it didn't work.

Here's my code:

public string Name { get; private set; }
public int Score { get; set; }
public decimal Average { get; set; }
public decimal Count { get; set; }
public int Star { get; set; }

public Person(string name, int score, decimal avg, int count, int star)
{
    Name = name;
    Score = score;
    Average = avg;
    Count = count;
    Star = star; 
}
public ObservableCollection<Person> Persons { get; set; }
filepath = scoresFile; 

public MainWindow()
{
    InitializeComponent();
    LoadTable();
}

private void datagrid_Sorting(object sender, DataGridSortingEventArgs e)
{
    if (e.Column.Header.Equals("Name") || e.Column.Header.Equals("Score"))
    {
        e.Handled = true;
    }
}

public void LoadTable()
{
    var lines = File.ReadAllLines(filepath);
    Persons = new ObservableCollection<Person>();
    var data = lines.Select(line =>
    {
    var column = line.Split(',');
    int c = column[1].Count(); 
    var name = column[1];
    int score = int.Parse(column[3]);
    decimal avg = decimal.Parse(column[3]);
    int count = 0; 
    int star = 0; 
    char letter;
    return new { name, score, avg, count, star};
    });

    var groupedData = data.GroupBy(p => p.name)
        .Select((g, i) => new { 
                num= 0, name = g.Key, 
                score = g.Sum(p => p.score), 
                avg = decimal.Round(g.Average(p => p.avg), 1), 
                star = g.Count(p => p.score == 5) })
        .OrderByDescending(x => x.avg);

        if (groupedData.Where(u => u.star == 3 ))
        {
          letter = "A"
        }
        else if (groupedData.Where(u => u.star == 2 ))
        {
         letter = "B"
        }
        else
        {
          letter = "C"
        }

    var persons = groupedData.Select((p, i) => new Person(i + 1, p.name, p.score, p.avg, p.count, p.star));

    foreach (var person in persons)
    {
        Persons.Add(person);
    }

    datagrid.ItemsSource = Persons; 
}

This is the new line I added which didn't work with me:

if (groupedData.Where(u => u.star == 3 ))
{
    letter = "A"
}
else if (groupedData.Where(u => u.star == 3 ))
{
    letter = "B"
}
else
{
    letter = "C"
}

Any idea what I am doing wrong? Perhaps I am not calling the LINQ query correctly within the if statement?

Upvotes: 0

Views: 186

Answers (1)

Saadi
Saadi

Reputation: 2237

Update your groupedData LINQ statement with this code:

var groupedData = data.GroupBy(p => p.name)
                            .Select((g, i) => new {
                                num = 0,
                                name = g.Key,
                                score = g.Sum(p => p.score),
                                avg = decimal.Round(g.Average(p => p.avg), 1),
                                star = g.Count(p => p.score == 5),
                                letter = g.Count(p => p.score == 5) == 3 ? "A" : (g.Count(p => p.score == 5) == 2 ? "B" : (g.Count(p => p.score == 5) == 1 ? "C" : ""))
                            })
                            .OrderByDescending(x => x.avg);

Upvotes: 1

Related Questions