c# - A local or parameter named 'g' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter -


i'm using linq groupby make groups result. ok of them i'm getting strange error when try group year value of datetime.

   switch (grouping)          {              case "country":                  vix = vi.groupby(v => v.user.countryid).select(g => g.tolist()).tolist();                  break;              case "province":                  vix = vi.groupby(v => v.user.provinceid).select(g => g.tolist()).tolist();                  break;              case "city":                  vix = vi.groupby(v => v.user.cityid).select(g => g.tolist()).tolist();                  break;              case "region":                  vix = vi.groupby(v => v.user.regionid).select(g => g.tolist()).tolist();                  break;              case "education":                  vix = vi.groupby(v => v.user.educationid).select(g => g.tolist()).tolist();                  break;              case "job":                  vix = vi.groupby(v => v.user.jobid).select(g => g.tolist()).tolist();                  break;              case "gender":                  vix = vi.groupby(v => v.user.gender).select(g => g.tolist()).tolist();                  break;              case "age":                  vix = vi.groupby(v => v.user.birthdate.value.year).select(g => g.tolist()).tolist();                  break;          } 

this error (error last groupby age):

a local or parameter named 'g' cannot declared in scope because name used in enclosing local scope define local or parameter

right i'm not able verify if compiles ;-) simplifying code this?

igrouping<typeofvi> group; // replace actual type switch (grouping) {     case "country":         group = vi.groupby(v => v.user.countryid);         break;     case "province":         group = vi.groupby(v => v.user.provinceid);         break;     // , on... } vix = group.select(g => g.tolist()).tolist(); 

just try pull select out of switch-case which, way, reduces code repetition.


Comments