0

I'm implementing asp.net core project and I have a line of code like following:

var myAPIApplicantId = (from x in _context.Apiapplicant
                        where x.ApiRequestNo == apiapplicantviewmodel.apiApplicantvm.ApiRequestNo
                        select new { x.Id }).First();

After running the project I understand myAPIApplicantId contains { Id = 1148 }. In another line of code, I have the following code:

int IntAPIApplicantID = Convert.ToInt32(myAPIApplicantId);

But it throws an error for type casting. Now I want to know how cant I get 1148 value to convert to int?

2
  • 2
    This is in no way a valid question. If the value of myAPIApplicantId is an interger, why convert to an int. Even if it is a string, the line of code Convert.ToInt32 must work. If it is an object, the access the Id properly with myAPIApplicantId.Id Commented Feb 22, 2020 at 13:55
  • Thank you. Yes your answer is ok. But you answered me a loittle bit late because I had solved my issue. Commented Feb 26, 2020 at 10:24

1 Answer 1

1

You could get the id like below:

var myAPIApplicantId = (from x in _context.Apiapplicant
                    where x.ApiRequestNo == apiapplicantviewmodel.apiApplicantvm.ApiRequestNo
                    select new { x.Id }).First();   //this generate an Anonymous Type like {Id=1}

int IntAPIApplicantID = myAPIApplicantId.Id;

The other way,you could change like below:

var myAPIApplicantId = _context.Apiapplicant
                               .Where(x.ApiRequestNo == apiapplicantviewmodel.apiApplicantvm.ApiRequestNo)
                               .Select(x => x.Id).First(); //this generate a Integer type

int IntAPIApplicantID = myAPIApplicantId; 
Sign up to request clarification or add additional context in comments.

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.