0

I was creating my api to return cod_postal from any city to make some linked dropdown combos.

I realize in db some cod_postal are duplicated. So I try to remove it using GroupBy

Was getting some error until I found a sample working with a list. Sample

So I decide first create a List<dto> and then perform the group by and that solve my duplicated problem as the code show.

The question is why I need that .ToList() to join DTO and GroupBy steps?

public class cod_postalDTO
{
    public int cod_postal_id { get; set; }
    public string name { get; set; }
}
public class codPostalController : ApiController
{
    private dbEntities db = new dbEntities ();

    public List<cod_postalDTO> Getcod_postal(int city_id)
    {

        List<cod_postalDTO> l_cod_postal = db.cod_postal
                                            .Where(c => c.city_id == city_id)
                                            .Select(c => new cod_postalDTO
                                            {
                                                cod_postal_id = c.cod_postal_id,
                                                name = c.name
                                            })
                                            .OrderBy(o => o.name)
                                            .ToList() // <== why i need this line?
                                            .GroupBy(c => c.name)
                                            .Select(grp => grp.First())
                                            .ToList();
        
        return l_cod_postal;
    }

If I don't include that middle .ToList got the following error

El método 'First' sólo se puede usar como operación de consulta final. Considere la posibilidad de utilizar en su lugar el método 'FirstOrDefault' en esta instancia.

This is something like the 'First' method can only be use as final operation, consider use 'FirstOrDefault' instead.

But 'FirstOrDefault' doesn't work here neither

Community
  • 1
  • 1
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
  • Are you trying to implicitly cast a [Enumerable](https://msdn.microsoft.com/en-us/library/system.linq.enumerable%28v=vs.100%29.aspx) to a List? – lloyd Jun 04 '15 at 16:50
  • What makes you think you need it? And you certainly don't need both `OrderBy` since you are already using `GroupBy`. Also, you were looking for the [`Distinct`](https://msdn.microsoft.com/en-us/library/vstudio/bb348436.aspx) method. – John Saunders Jun 04 '15 at 16:51
  • Nope. IEnumerable also have .GroupBy(). As you can see the last line is also a .ToList() – Juan Carlos Oropeza Jun 04 '15 at 16:51

2 Answers2

4

I get an error

That's not a very helpful problem description. I can guess the problem though: ToList transitions the query from LINQ to SQL or EF (you haven't said what ORM you are using) to LINQ to Objects. Your LINQ provider probably didn't like that particular pattern and couldn't translate it. LINQ to Objects can execute anything but it does so on the client.

usr
  • 168,620
  • 35
  • 240
  • 369
  • Yes, Im using EF. So when I do the first `.ToList` I'm moving my db data to LINQ to Object instead of LINQ to Provider? And that one can handle the grp.First() properly? (Hope I got the terminolgy right) – Juan Carlos Oropeza Jun 04 '15 at 17:02
  • Correct. At that point the type of the expression is IEnumerable which causes the Queryable.* methods to no longer be applicable. – usr Jun 04 '15 at 17:04
0

Intead of using groupby you should use distinct

pmeyer
  • 890
  • 7
  • 31
  • There are two properties cod_postal_id (unique value) and name (posible duplicate) How i make Distinct bring the first pair of the duplicated ? – Juan Carlos Oropeza Jun 04 '15 at 18:37