c# - Why I need ToList before GroupBy -


i creating api return cod_postal city make linked dropdown combos.

i realize in db cod_postal duplicated. try remove using groupby

was getting error until found sample working list. sample

so decide first create list<dto> , perform group , solve duplicated problem code show.

the question why need .tolist() join dto , 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 need line?                                             .groupby(c => c.name)                                             .select(grp => grp.first())                                             .tolist();          return l_cod_postal;     } 

if don't include middle .tolist got 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 'first' method can use final operation, consider use 'firstordefault' instead.

but 'firstordefault' doesn't work here neither

i error

that's not helpful problem description. can guess problem though: tolist transitions query linq sql or ef (you haven't said orm using) linq objects. linq provider didn't particular pattern , couldn't translate it. linq objects can execute on client.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -