Search code examples
c#entity-frameworksyntaxvariable-declaration

Explicit variable declaration


I have a local variable implicitly defined as var, and is being populated with objects retrieved from a database through Entity Framework. When I hover over the variable I get the details as shown in the screenshot here:

enter image description here

How can I explicitly define my variable without using var, for example

IQueryable<{Inspection Ins, Field F}> tempInspInner =  getInspections();

Instead of:

var tempInspInner =  getInspections();

UPDATE

getInspections() has the following code:

return _dbcontext.Inspection
                .Join(_dbcontext.Field,
                      ins => ins.FieldId,
                      f => f.FieldId,
                      (ins, f) => new { Ins = ins, F = f }).Where(*hidden*);

Solution

  • getInspections returns an anonymous type (meh sigh), named tuples wont help, however you could project it to a class

    public class SomeObject 
    {
        public Inpection Ins {get;set;}
        public Field F {get;set;}
    }
    
    IQueryable<SomeObject> = getInspections.Select(x => new SomeObject { Ins = x.Ins, F = x.F });
    

    On saying this, you probably be better return the a strongly typed IQueryable anyway