Linq分配给List<>列表绑定

时间:2012-07-26 18:50:22

标签: linq entity-framework list binding

我在运行时遇到此错误:

  

异常详细信息:System.InvalidOperationException:未处理的绑定类型:ListBinding

  • Choice_A,_b,_C是字符串
  • '选择'是List

  var qs = (from questions in dc.Survey_Questions
                      where questions.Survey_ID == surveyid                     
                      select new SQuestions
                      {
                          QuestionID = questions.Question_ID,                          
                          Description = questions.Description,
                          Choice_A = questions.Choice_A,  
                          Choice_B = questions.Choice_B,  
                          Choice_C = questions.Choice_C,
                         **Choices =  {questions.Choice_A, questions.Choice_B,
          questions.Choice_C}**
   }).ToList();

基本上我想知道如何分配Choice_A,Choice_B,Choice_C的List Choices值。 提前谢谢。

2 个答案:

答案 0 :(得分:4)

你可以试试这个。如果您已经有以下两个类

public class Question
{
    public int Question_ID { get; set; }
    public int Survey_ID { get; set; }

    public String Description { get; set; }
    public String Choice_A { get; set; }
    public String Choice_B { get; set; }
    public String Choice_C { get; set; }
}

public class SQuestions
{
    public int QuestionID { get; set; }
    public String Description { get; set; }
    public String Choice_A { get; set; }
    public String Choice_B { get; set; }
    public String Choice_C { get; set; }

    public List<String> Choices { get; set; }
}
  

然后LINQ查询将

var qs = (from question in dc.Survey_Questions
where question.Survey_ID == surveyid
select new SQuestions
{
   QuestionID = question.Question_ID,
   Description = question.Description,
   Choice_A = question.Choice_A,
   Choice_B = question.Choice_B,
   Choice_C = question.Choice_C,
   Choices = new List<string>(new String[] { question.Choice_A,question.Choice_B, question.Choice_C })
}).ToList();

答案 1 :(得分:0)

LINQ to Entities不支持此绑定语法。不过你应该能够做到这一点:

qs = (from questions in dc.Survey_Questions
                  where questions.Survey_ID == surveyid                     
                  select new SQuestions
                  {
                      QuestionID = questions.Question_ID,                          
                      Description = questions.Description,
                      Choice_A = questions.Choice_A,  
                      Choice_B = questions.Choice_B,  
                      Choice_C = questions.Choice_C,
                     Choices = new List<string> {questions.Choice_A, questions.Choice_B,
      questions.Choice_C}
}).ToList();