-2

我有这个 sql 语句:

 SELECT questionNo , ActivityID ,GROUP_CONCAT(CONCAT(Question, '[' , Answer , ']')
 SEPARATOR ' ')AS joined
 FROM question
 GROUP BY QuestionNo , ActivityID
 LIMIT 0,30

但是我正在使用 MySQL 的实体框架,并且我的大多数类文件方法都是 lambda,我如何将其转换为 lambda 表达式?

以下 - - - - - - -

public IList<Model.question> GetList()
    {
        IList<Model.question> lstRecords = context.questions.ToList();
        return lstRecords.GroupBy(x => new { x.QuestionNo, x.ActivityID }).Select(g => new { QuestionNo = g.Key.QuestionNo, ActivityID = g.Key.ActivityID, joined = string.Join(" ", g.Select(i => i.QuestionContent + "[" + i.Answer + "]"))});

    } 

如果我想获取它的列表,这就是我想写的方式?它说不能将类型 ienumerable 转换为 ilist 。

4

1 回答 1

3

试试下面

var result = question.GroupBy(x=>new{x.QuestionNo , x.ActivityID})
.Select(g=> new {QuestionNo= g.Key.QuestionNo, 
                 ActivityID= g.Key.ActivityID,
                 joined = string.Join(" ", g.Select(i=>i.Question+ "[" + i.Answer +"]"))})
.Take(30);

如果你想从方法返回这个,你可以创建如下的新类

public class Question
{
    public int QuestionNo {get;set;} //change the type accordingly 
    public string ActivityID {get;set;} //change the type accordingly 
    public string joined {get;set;}   //change the type accordingly 
}

并将方法更改为:

public IList<Question> GetList()
{
    IList<Question> lstRecords = context.questions.GroupBy(x => new { x.QuestionNo, x.ActivityID })
        .Select(g => new Question(){
              QuestionNo = g.Key.QuestionNo, 
              ActivityID = g.Key.ActivityID, 
              joined = string.Join(" ", 
                     g.Select(i => i.QuestionContent + "[" + i.Answer + "]"))})
       .Take(30)
       .ToList();
} 
于 2013-06-04T07:26:41.537 回答