如何使用LINQ在列表中添加列表

时间:2014-08-04 10:50:54

标签: c# linq wcf list

我在WCF服务中有一个返回问题列表的方法,以及这些问题是可以选择的一些选项(答案)。

然后将选项添加到客户端应用程序上的RadioButtonList,以便客户端可以针对特定问题进行适当选择。

我的问题是如何在列表中添加我将返回客户端应用程序的列表。

我想返回特定问题的选项列表。

这是我到目前为止所拥有的,

public class ct_auditResults
{
    private int AuditResultsID { get; set; }
    private int QuestionID { get; set; }
    private string Question { get; set; }
    private string QuestionValue { get; set; }
    private int QuestionTypeID { get; set; }
    private bool IsOptional { get; set; }
    private bool AllowComments { get; set; }
    private bool AllowActions { get; set; }
    private List<QuestionOptionText> OptionText { get; set; }

    public int auditResultsID
    {
        get { return AuditResultsID; }
        set { AuditResultsID = value; }
    }

    public int questionID
    {
        get { return QuestionID; }
        set { QuestionID = value; }
    }

    public string question
    {
        get { return Question; }
        set { Question = value; }
    }

    public string questionValue
    {
        get { return QuestionValue; }
        set { QuestionValue = value; }
    }

    public int questionTypeID
    {
        get { return QuestionTypeID; }
        set { QuestionTypeID = value; }
    }

    public bool isOptional
    {
        get { return IsOptional; }
        set { IsOptional = value; }
    }

    public bool allowComments
    {
        get { return AllowComments; }
        set { AllowComments = value; }
    }

    public bool allowActions
    {
        get { return AllowActions; }
        set { AllowActions = value; }
    }

    public List<QuestionOptionText> optionText
    {
        get { return OptionText; }
        set { OptionText = value; }
    }


    public class QuestionOptionText
    {
        private string QuestionText { get; set; }

        public string questionText
        {
            get { return QuestionText; }
            set { QuestionText = value; }
        }
    }
}

class AuditResultsClass : IAuditResults
{
    CTDBDataContext db = new CTDBDataContext();

    public List<ct_auditResults> getQuestionsForSubSectionAudit(long _SiteAuditID, long _SubSectionID)
    {
        List<ct_auditResults> QuestionList = (from ar in db.AuditResults
                                                join q in db.Questions on ar.QuestionID equals q.QuestionID
                                                join qt in db.QuestionTypes on q.QuestionTypeID equals qt.QuestionTypeID
                                                join opt in db.Options on q.QuestionID equals opt.QuestionID
                                                where ar.SiteAuditID == _SiteAuditID &&
                                                    ar.SubSectionID == _SubSectionID
                                                select new ct_auditResults
                                                {
                                                    auditResultsID = ar.SiteAuditID,
                                                    questionID = ar.QuestionID,
                                                    questionValue = ar.QuestionValue,
                                                    questionTypeID = q.QuestionTypeID,
                                                    question = q.Question1,
                                                    isOptional = q.IsOptional,
                                                    allowComments = q.AlowComments,
                                                    allowActions = q.AllowActions,
                                                    // I would like to return the option list List<QuestionOptionText> OptionText
                                                }).ToList();
        return QuestionList;
    }
}

提前谢谢。

2 个答案:

答案 0 :(得分:2)

假设该表名为QuestionOptionTexts。然后你可以这样做:

question = q.Question1,
isOptional = q.IsOptional,
allowComments = q.AlowComments,
allowActions = q.AllowActions,
OptionText = db.QuestionOptionTexts
             .Where(w=>w.QuestionID==q.QuestionID)
             .Select(s=>new QuestionOptionText(){questionText=s.QuestionText})
             .ToList()
}).ToList();

答案 1 :(得分:0)

您可以使用以下选项展开选择:

select new ct_auditResults
{
  ....
  optionText = some_query.ToList()
}