Losó Adam
Losó Adam

Reputation: 548

Linq query result changing not working

var query = from table1 in dtSzotar
            where errorList.Any(word => table1.angol.Equals(word)) ||
                  errorList.Any(word=> table1.magyar.Equals(word))
            select new QueryObject
            {
                Lesson = table1.lesson,
                English = table1.angol,
                Hungarian = table1.magyar,
                Answer = "",
            };
foreach (QueryObject row in query)
{
      String tmp;
      ((App)Application.Current).Answer.TryGetValue(row.English, out tmp); 
      row.Answer = tmp;
}

dgInCorrect.ItemsSource = query.ToList();

I'm trying to display wrong user's answers in the datagrid too. tmp contains the answer, but it's not added to row.Answer. Also There's an extra empty row at the end of the datagrid. What am i doing wrong?

Here's QueryObject.cs:

class QueryObject
{
    private int lesson;
    private String english;
    private String hungarian;
    private String answer;

    public int Lesson { get { return lesson; } set { lesson = value; } }
    public String Hungarian { get { return hungarian; } set { hungarian= value; } }
    public String English{ get { return english; } set { english= value; } }
    public String Answer{ get { return answer; } set { answer= value; } }
}

Upvotes: 0

Views: 82

Answers (1)

Ahmad Mageed
Ahmad Mageed

Reputation: 96497

You're looping over an IEnumerable<QueryObject>. Instead, turn your query into a list earlier, then it should work:

var query = (from table1 in dtSzotar
            where errorList.Any(word => table1.angol.Equals(word)) ||
                  errorList.Any(word=> table1.magyar.Equals(word))
            select new QueryObject
            {
                Lesson = table1.lesson,
                English = table1.angol,
                Hungarian = table1.magyar,
                Answer = "",
            }).ToList();

// now query is a List<QueryObject>
foreach (QueryObject row in query)
{
      String tmp;
      ((App)Application.Current).Answer.TryGetValue(row.English, out tmp); 
      row.Answer = tmp;
}

dgInCorrect.ItemsSource = query;

Upvotes: 1

Related Questions