0
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();

我也试图在数据网格中显示错误的用户答案。tmp包含答案,但未添加到row.Answer. 在数据网格的末尾还有一个额外的空行。我究竟做错了什么?

这是 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; } }
}
4

1 回答 1

1

你正在循环一个IEnumerable<QueryObject>. 相反,先将您的查询转换为列表,然后它应该可以工作:

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;
于 2012-12-09T20:10:26.520 回答