1

我有以下

public class AnswerDetail
{
    public int AnswerId { get; set; }
    public string Text { get; set; }
    public Nullable<bool> Correct { get; set; }
    public Nullable<bool> Response { get; set; }
}
  • 我从客户端收到了一个 AnswerDetail,其中填充了 Response 字段:AnswerDetail rfc
  • 从数据库中,我收到了一个 AnwerDetail,其中填充了正确的字段:AnswerDetail afd

如何将这些组合起来以创建另一个 AnswerDetail 对象,该对象从 afd 获取 AnswerId、Text 和 Correct 以及从 rfc 获取响应?

更新:删除 ClientAnswers 类以简化问题

4

7 回答 7

1

您需要创建一个新的AnswerDetail,然后只需从正确的源设置属性。

AnswerDetail adCombined = new AnswerDetail
{
    AnswerId = afd.AnswerId,
    Text = afd.Text,
    Correct = afd.Correct,
    Response = rfc.Response
};
于 2013-09-18T09:27:57.900 回答
1
AnswerDetail ans = new AnswerDetail{Correct = rfc.Correct, Response = afd.Response}
于 2013-09-18T09:31:15.940 回答
0
rfc.Select(r=>
    new AnswerDetail
    {
        AnswerId =r.AnswerId,
        Text=r.Text,
        Response=r.Response,
        Correct=afd.Single(c=>c.AnswerId==r.AnswerId).Correct
    }
           );
于 2013-09-18T09:25:37.560 回答
0

也许你可以试试这个:

var combined = from rfcItem in rfc.Answers
               join afdItem in afd.Answers on rfcItem.AnswerId equals afdItem.AnswerId
               select new AnswerDetail 
               { 
                   AnswerId = afdItem.AnswerId,
                   Text = afdItem.Text,
                   Correct = afdItem.Correct,
                   Response = rfcItem.Response
               };
于 2013-09-18T09:26:12.173 回答
0

好吧,鉴于您已经拥有Response来自客户端的字段,这实际上只是用数据库中的正确答案更新结果的情况,例如

foreach (var a in rfc)
{
    a.Correct = afd.Answers.Single(x => x.AnswerId == a.AnswerId).Select(x => x.Correct);
}
于 2013-09-18T09:27:23.367 回答
0

我想每个 afd 总是有对应的 rfc 答案:

var zipped = from rows in afd.Answers
             select new AnswerDetail()
             {
                AnswerId = rows.AnswerId,
                Correct = rows.Correct,
                Response = rfc.Answers.First(r=>r.AnswerId == rows.AnswerId).Response,
                Text = rows.Text
             };

您可以将整个 ClientAnswer 创建为:

var zipped = new ClientAnswers()
             {
                QuestionId = yourQuestionId, 
                Answers = (from rows in afd.Answers
                          select new AnswerDetail()
                          {
                              AnswerId = rows.AnswerId,
                              Correct = rows.Correct,
                              Response = rfc.Answers.First(r => r.AnswerId == rows.AnswerId).Response,
                              Text = rows.Text
                          }).ToList()
            };
于 2013-09-18T09:29:24.293 回答
0

你的信息没有提到列表。

为什么不:

afd.Response = rfc.Response;
于 2013-09-18T09:39:29.513 回答