-1

In my previous code, I try in the view. I think I will need to check first if the id is empty or null then set the value equal to 0 but I got an error, says, accessing local variable is not allow. But how can I access a local variable of var inside of if statement?

var Rs1;
if (id != null)
{
    Rs1 = _context.DwPropertyDetails.Select(x => new
    {
        Rs1Clm2 = totalTransAmount,
        Rs1Clm3 = x.Studio),
        Rs1Clm4 = x.OneBedroom),
        Rs1Clm5 = x.TwoBedroom),
        Rs1Clm6 = x.ThreeBedroom),
        Rs1Clm7 = x.Total),
    });
}
else
{
    Rs1 = _context.DwPropertyDetails.Select(x => new
    {
        Rs1Clm2 = 0,
        Rs1Clm3 = 0,
        Rs1Clm4 = 0,
        Rs1Clm5 = 0,
        Rs1Clm6 = 0,
        Rs1Clm7 = 0,
    });
}
Mikhail Tulubaev
  • 4,141
  • 19
  • 31
  • What's the actual type? – ColinM Feb 20 '17 at 08:41
  • 3
    Is `var Rs1;` even allowed? – Amit Kumar Ghosh Feb 20 '17 at 08:41
  • For *anonymous type* there is an [ugly workaround](http://stackoverflow.com/a/4708589/1997232) to define its type (for use in `List`). You can't use `var` in such scenario, as it need type to be extract-able from right side of expression. I believe `dynamic` will do, but it comes without intellisense support. – Sinatr Feb 20 '17 at 08:51

3 Answers3

2

var infers the type. If you don't assign a value, it can't infer the type of the value.

You have to create a class or struct and put your fields in there. Then use the type of the created class.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

If you really want to use var:

 var Rs1 = id != null ?
             _context.DwPropertyDetails.Select(x => new
                {
                    Rs1Clm2 = totalTransAmount,
                    Rs1Clm3 = x.Studio,
                    Rs1Clm4 = x.OneBedroom,
                    Rs1Clm5 = x.TwoBedroom,
                    Rs1Clm6 = x.ThreeBedroom,
                    Rs1Clm7 = x.Total,
                })
           : _context.DwPropertyDetails.Select(x => new
                {
                    Rs1Clm2 = 0,
                    Rs1Clm3 = 0,
                    Rs1Clm4 = 0,
                    Rs1Clm5 = 0,
                    Rs1Clm6 = 0,
                    Rs1Clm7 = 0,
                 });
bcl
  • 147
  • 4
  • 15
  • Why not just use the null coalescing operator instead of using the conditional operator? Like so - `Rs1Clm3 = x?.Studio ?? 0,` – ColinM Feb 20 '17 at 10:12
-1

Why not use object instead of var?

var needs to be implicitly typed first. But, since you're assigning an object without a type, might as well just use 'object'.

E.g. object Rs1;

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
mindOfAi
  • 4,412
  • 2
  • 16
  • 27
  • Yes, Stephen, you should try this – Lali Feb 20 '17 at 08:41
  • 2
    Using `object` doesn't allow accessing the properties, so it renders the object useless for the developer. – Patrick Hofman Feb 20 '17 at 09:02
  • 1
    *"Why not use `object` instead of `var`?"* - for two reasons: boxing (minus performance) and no intellisense support (minus comfort). Moreover, as here is anonymous type involved, using `object Rs1` makes it completely useless for later, unless you going to cast, but then either you have a concrete type or it's pretty [ugly](http://stackoverflow.com/a/1409776/1997232). – Sinatr Feb 20 '17 at 09:15