Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
error CS0266: Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type: string Name>>' to 'System.Collections.Generic.IEnumerable<System.Collections.Generic.List<ProductsApi.Models.Product>>'. An explicit conversion exists (are you missing a cast?)


What I have tried:

public  IEnumerable<IGrouping<String,Product>> GetProducts4()
        {
            var group =(from p in _context.Products
                        group p by p.Name
                        into g
                    select new 
                    {
                        Name = g.Key
                    }).ToList();
            return group;
        }
Posted
Updated 17-Jul-22 22:22pm
v4
Comments
Richard MacCutchan 12-Jul-22 3:43am    
You are returning a List<String> from a method that is supposed to return an IEnumerable<IGrouping<String,Product>>.
Richard Deeming 19-Jul-22 4:44am    
If you have another question, then post a new question.

Editing your existing question to completely change what you're asking is just rude.

1 solution

Simple: don't project the group before returning it. (You are currently only selecting the group's key, which doesn't match your return type.)
C#
public  IEnumerable<IGrouping<String,Product>> GetProducts4()
{
    return from p in _context.Products
           group p by p.Name into g
           select g;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900