简体   繁体   中英

C# How do i return iGrouping to a list using lambda expression

I am using entity framework to do this , i have this lambda expression :

public IList<Model.questionhint> GetRecordsPlease(int listTask, int listActivity)
{
   IList<Model.questionhint> lstRecords = context.questionhints.ToList();
   return lstRecords.GroupBy(x => new { 
                                      x.QuestionNo, 
                                      x.ActivityID, 
                                      x.TaskID })
                    .Where(a => a.Key.TaskID == listTask 
                             && a.Key.ActivityID == listActivity)
                    .ToList();         
}

but it says :

Cannot implicitly convert type ' System.Collections.Generic.List<System.Linq.IGrouping<AnonymousType#1,iStellar.Model.questionhint>> ' to ' System.Collections.Generic.IList<iStellar.Model.questionhint> '. An explicit conversion exists (are you missing a cast?)

This codes is in my CRUD class file named DAOQuestionHint .

How do i return this because i need to call it in my xaml.cs ( code behind ) like this :

private DAO.DAOQuestionHint qh = new DAO.DAOQuestionHint();

IList<Model.questionhint> lstQuestionHints = qh.GetRecordsPlease(taskID, activityID);

This is how my database table looks like :

在此处输入图片说明

what i wanted is to group records of the same activityID , taskID and questionNo together in a line , now every record is seperated line by line like this :

在此处输入图片说明

i want it to be like

I [answer] to have a nap every afternoon

The sun [answer] not move round the earth.

So i tried to use alternative in code behind by using for loop but that doesn't work perfectly if there are 3 or more records of the same activityID , taskID and questionNo , so i tot i need to group them in the query first .

It seems to me that you don't need to group at all - you just need to filter. Unless you really have more than one hint per question (with the same task and activity), you just need:

public IList<Model.questionhint> GetRecordsPlease(int listTask, int listActivity)
{
    return context.questionhints
                  .Where(a => a.TaskID == listTask && a.ActivityID == listActivity)
                  .ToList();
}

Note that unlike your current code, this will perform the filtering in the database, rather than pulling the whole table to the client and then grouping/filtering. You need to be aware that any time you use context.SomeTable.ToList() , that will pull the entire contents of that table.

I'd also strongly suggest that you change your model to use type names which follow .NET naming conventions, so QuestionHint instead of questionhint .

(Finally, I'd remove the Please from the method name - computers don't need you to be polite in your naming like this.)

EDIT: If you really do want groups, you need to change the return value. For example, you could just return a sequence of groups:

public IList<IGrouping<int, Model.questionhint>>
     GetRecordsPlease(int listTask, int listActivity)
{
    return context.questionhints
                  .Where(a => a.TaskID == listTask && a.ActivityID == listActivity)
                  .ToList()
                  .GroupBy(a => a.QuestionNo)
                  .ToList();
}

I've deliberately called ToList() here before the grouping and then again afterwards so that the data is fetched once and you can iterate over those groups however you like without it going back to the database. (It's possible that just the final ToList would be enough, but I wouldn't like to say for sure.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM