0

So i am trying to have a ternary operator inside the viewmodel. NOTE: RegionId is int. This is the code

 var model = (from p in _context.ProductGun
              where p.ProductId == productId
              select new GunViewModel)
              {
                  RegionId = p.RegionId == 1 ? "DEV" : "TEST"; //error
              }

I've tried: RegionId = p.RegionId.ToString() == "1" ....; RegionId = p.RegionId == Convert.ToInt32(1); And vice versa solutions. But nothing is correct. Sorry for the typo. I type this using mobile

3
  • You cannot convert "DEV" to int... Commented Apr 23, 2018 at 21:26
  • If RegionId is an int, why do you think its ok to assign it "DEV"? "DEV" is a string, not an int Commented Apr 23, 2018 at 21:29
  • should it be from p in _context.ProductGun where p.ProductId == productId select new GunViewModel { RegionId = p.RegionId == 1 ? "DEV" : "TEST"; //error } the ) is in the wrong place? Commented Apr 23, 2018 at 22:06

2 Answers 2

1

The error is telling you that RegionId is an Int and your trying to assign "DEV" to it anytime the region id is a 1. thats not going to work. try putting a zero in it and checking for that anywhere you are looking for "DEV"

RegionId = p.RegionId == 1 ? 0 : p.RegionId;
Sign up to request clarification or add additional context in comments.

Comments

0

This is the answer:

I changed the datatype of RegionId in Viewmodel to String. Since it is int to int, It cannot implicitly convert type.

RegionId = p.RegionId == 1 ? "DEV" : "TEST"; Where: RegionId = string p.RegionId = int

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.