c# - Cannot implicitly convert anonymous type #1[] to anonymous type #2[] -


i have following scenario:

public jsonresult changefilterlist(int option) {     var data = new[] { new { text = "unknown option", value = -1 } };     switch (option)     {         case 2: data = _departmentnamerepository.all.select(x => new { text = x.deptname, value = x.id }).toarray();             break;         case 3: data = session["projectid"] == null                 ? _assetsequencerepository.all.select(x => new { text = x.assetshotname, value = x.id }).toarray()                 : _assetsequencerepository.findby(p => p.projectid == (int)session["projectid"]).select(x => new { text = x.assetshotname, value = x.id }).toarray();             break;         default: data = _userrepository.all.select(x => new { text = x.displayname, value = x.userid }).toarray();             break;     }                  return json(data, jsonrequestbehavior.allowget); } 

case2 , default looks great complains on case 3 (conditional) saying: cannot implicitly convert type 'anonymoustype#1[]' 'anonymoustype#2[]'. shouldn't ?: able decide type since have provided blueprint anonymous var data = new[] { new { text = "unknown option", value = -1 } };.

solution:

@darin dimitrov answer great want have test anonymous types (simple cases need it). @douglas suspects : assetsequencerepository supplying id long , anonymous value goes in favor of int not long. since c# compiler not implicitly cast long int, got error. compiling snippet is:

public jsonresult changefilterlist(int option = 3)         {             var data = new[] { new { text = "unknown option", value = long.maxvalue } };             switch (option)             {                 case 2: data = _departmentnamerepository.all.select(x => new { text = x.deptname, value = (long)x.id }).toarray();                     break;                 case 3: data = session["projectid"] == null                         ? _assetsequencerepository.all.select(x => new { text = x.assetshotname, value = x.id }).toarray()                         : _assetsequencerepository.findby(p => p.projectid == (int)session["projectid"]).select(x => new { text = x.assetshotname, value = x.id }).toarray();                     break;                 default: data = _userrepository.all.select(x => new { text = x.displayname, value = (long)x.userid }).toarray();                     break;             }                          return json(data, jsonrequestbehavior.allowget);         } 

my guess findby method returns objects properties have different types ones you're expecting (e.g. int? instead of int). try specifying type cast ensure anonymous type has correct definition:

case 3: data = session["projectid"] == null              ? _assetsequencerepository.all.select(x => new { text = x.assetshotname, value = x.id }).toarray()              : _assetsequencerepository.findby(p => p.projectid == (int)session["projectid"]).select(x => new { text = (string)x.assetshotname, value = (int)x.id }).toarray();         break; 

the key change is:

new { text = (string)x.assetshotname, value = (int)x.id })                   ↖    explicit type casts    ↗ 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -