Search code examples
c#implicit-declaration

Access implicitly typed variable outside the block


Im trying to declare a variable as an object which returns an anonymous type object. I get an error like (cannot implicitly convert type Anonymous1 to Anonymous2.

var queryResult = new object();

            if (check == 1)
            {
                queryResult = context.SubjectContext
                    .Where(x => x.CreatedBy == CreatedBy)
                    .Select(x => new { x.Id, x.Name });
            }
            else (check == 2)
            {
                queryResult = context.SubjectContext
                    .Where(x => x.CreatedBy == CreatedBy)
                    .Select(x => new { x.Id, x.Name, x.Skillset });
            }


            if (queryResult.Any())
            {
                return jsonRetrunMsg = new JsonReturn { Status = "success", Message = queryResult };
            }
            else
            {
                return jsonErrorMsg = new JsonError { Status = "error", Message = "Check User Id" };
            }

Solution

  • Although you are selecting from the same context, in this case 'SubjectContext', you are creating a new type, when doing this:

    .Select(x => new { x.Id, x.Name });
    

    and this;

    .Select(x => new { x.Id, x.Name, x.Skillset });
    

    So, in this case, you have to return to an implicity-typed variable, known as 'var'. Something like this:

    var queryResult = context.SubjectContext
            .Where(x => x.CreatedBy == CreatedBy)
            .Select(x => new { x.Id, x.Name });
    

    Of course, in this case - which you are using it between the brackets -, you have to change the declaration of the 'queryResult', and control the result, before returning. Something like this:

       if (check == 1)
       {
            var queryResult = context.SubjectContext
                .Where(x => x.CreatedBy == CreatedBy)
                .Select(x => new { x.Id, x.Name });
    
            if (queryResult.Any())
            {
                return jsonRetrunMsg = new JsonReturn { Status = "success", Message = queryResult };
            }
            else
            {
                return jsonErrorMsg = new JsonError { Status = "error", Message = "Check User Id" };
            }
    
        } else if (check == 0)
        {
            var queryResult = context.SubjectContext
                .Where(x => x.CreatedBy == CreatedBy)
                .Select(x => new { x.Id, x.Name, x.Skillset });
    
            if (queryResult.Any())
            {
                return jsonRetrunMsg = new JsonReturn { Status = "success", Message = queryResult };
            }
            else
            {
                return jsonErrorMsg = new JsonError { Status = "error", Message = "Check User Id" };
            }
        }
    

    In this case, the var queryResult will assume the type of the returned/created type in your Linq Query, and the error will not occour.

    Well, reading the comments, i came to another solution for you, i guess it'll work, just give it a shot.

    using System.Collections;
    object queryResult;
            if (check == 1)
            {
                queryResult = context.SubjectContext
                    .Where(x => x.CreatedBy == CreatedBy)
                    .Select(x => new { x.Id, x.Name });
    
            } else if (check == 0)
            {
                queryResult = context.SubjectContext
                    .Where(x => x.CreatedBy == CreatedBy)
                    .Select(x => new { x.Id, x.Name, x.Skillset });
            }
    
            if ((queryResult != null) && ((queryResult as ICollection).Count > 0))
            {
                return jsonRetrunMsg = new JsonReturn { Status = "success", Message = queryResult };
            }
            else
            {
                return jsonErrorMsg = new JsonError { Status = "error", Message = "Check User Id" };
            }
    

    Guess you can think about something to avoid this lot of code, but, it'll works for now. Victor.