如果我的对象与此语句底部的select匹配。有没有办法自动加载对象?
var qaResults = from q in allQuestions
join la in allLeaderAnswers on q.QuestionID equals la.QuestionID into leaderAnswers
from la in leaderAnswers.DefaultIfEmpty()
join ta in allTeacherAnswers on q.QuestionID equals ta.QuestionID into teacherAnswers
from ta in teacherAnswers.DefaultIfEmpty()
select new
{
QuestionID = q.QuestionID,
ID = q.ID,
QuestionText = q.Description,
LeaderAnswerText = la.Comment,
LeaderAnswerRating = (la.AnswerOptionKey == null ? 0 : la.AnswerOptionKey),
TeacherAnswerText = ta.Comment,
TeacherAnswerRating = (ta.AnswerOptionKey == null ? 0 : ta.AnswerOptionKey)
};
我的对象属性:
public int QuestionID { get; set; }
public string ID { get; set; }
public string QuestionText { get; set; }
public string LeaderAnswerText { get; set; }
public int LeaderRating { get; set; }
public string TeacherAnswerText { get; set; }
public int TeacherRating { get; set; }
我尝试了什么:
List<EvaluationResultSet> ResultSet = new List<EvaluationResultSet>();
ResultSet = qaResults.ToList();
答案 0 :(得分:3)
只需在select
select new EvaluationResultSet //Here
{
QuestionID = q.QuestionID,
ID = q.ID,
QuestionText = q.Description,
LeaderAnswerText = la.Comment,
LeaderAnswerRating = (la.AnswerOptionKey == null ? 0 : la.AnswerOptionKey),
TeacherAnswerText = ta.Comment,
TeacherAnswerRating = (ta.AnswerOptionKey == null ? 0 : ta.AnswerOptionKey)
};
目前您没有指定任何类名,因此它正在创建Anonymous Object。
如果您正在使用Entity框架或LINQ to SQL,那么只需确保您使用的类不是通过Entity framework / LINQ to SQL生成的类,因为您无法投影到生成的类,在这种情况下你必须定义一个新类。