Answered by:
IList GroupBy question

Question
-
User-492280791 posted
Hi All,
GroupBy returns an IGrouping< MyClass >
var groupdResult = result.GroupBy(x => x.MyField).ToList();
How to avoid this error:
Error 1 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.IList<ccc.Ets.Entities.ErrorMessageGroup>'. An explicit conversion exists (are you missing a cast?)
Thanks!
Sunday, September 20, 2015 10:40 AM
Answers
-
User-271186128 posted
Hi SpongeBert,
Can you do another test with IList instead of List? IList<StudentClass> list = new IList<StudentClass>();
In my test scenario I tried like this:
IList<ErrorMessageGroup> finalList2 = new List<ErrorMessageGroup>(); return finalList2.GroupBy(x => x.GroupingCriterion1Value);
Please try to use the following code:
IList<ErrorMessageGroup> finalList2 = new List<ErrorMessageGroup>(); return finalList2.GroupBy(x => x.GroupingCriterion1Value).Select(g => g.First()).ToList();
Best regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 21, 2015 9:35 PM
All replies
-
User-271186128 posted
Hi SpongeBert,
According to your code and description, I create a sample using the following code. They all worked well on my side. Please refer to them:
using (MyTestDBEntities context = new MyTestDBEntities()) { var query3 = customer.GroupBy(cc => cc.EmployeeID).ToList(); GridView2.DataSource = query3; GridView2.DataBind(); }
and
List<StudentClass> list = new List<StudentClass>(); for (int i = 1; i <= 5; i++) { StudentClass stu = new StudentClass(); stu.StuID = i; stu.StuName = "AAA" + i.ToString(); list.Add(stu); } var query4 = list.GroupBy(cc => cc.StuID).ToList(); GridView2.DataSource = query4; GridView2.DataBind();
They all display the key value, and didn't meet any error. So, I suppose perhaps the issue, is related to the result, I suggest you check it. If possible you could post more details information about the result.
Best regards,
DillionSunday, September 20, 2015 10:51 PM -
User-492280791 posted
Hi Dillion,
Thanks for your reply. Can you do another test with IList instead of List? IList<StudentClass> list = new IList<StudentClass>();
In my test scenario I tried like this:
IList<ErrorMessageGroup> finalList2 = new List<ErrorMessageGroup>(); return finalList2.GroupBy(x => x.GroupingCriterion1Value);
but I get following error:
Error 2 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Linq.IGrouping<string,Ccc.Eee.Entities.ErrorMessageGroup>>' to 'System.Collections.Generic.IList<Ccc.Eee.Entities.ErrorMessageGroup>'. An explicit conversion exists (are you missing a cast?)
Regards,
B
Monday, September 21, 2015 1:53 AM -
User-271186128 posted
Hi SpongeBert,
Can you do another test with IList instead of List? IList<StudentClass> list = new IList<StudentClass>();
In my test scenario I tried like this:
IList<ErrorMessageGroup> finalList2 = new List<ErrorMessageGroup>(); return finalList2.GroupBy(x => x.GroupingCriterion1Value);
Please try to use the following code:
IList<ErrorMessageGroup> finalList2 = new List<ErrorMessageGroup>(); return finalList2.GroupBy(x => x.GroupingCriterion1Value).Select(g => g.First()).ToList();
Best regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 21, 2015 9:35 PM