I have a struct named CheckedNumber
. This type is used in a property:
`public IList<CheckedNumber> CheckedNumbers { get { return this._checkedNumbers; } }`
...to keep a list of numbers.
I have another property:
`public int? TheNumber { get { return this._theNumber; } }`
...and want to check if its value exists in the CheckedNumbers
list.
this:
if (this.CheckedNumbers.Contains(this.TheNumber)) { //... }
gives error:
Argument 1: cannot convert from 'int?' to 'ProjectName.Models.CheckedNumber'
How could I convert int? to my struct type?
Okay, now the question is slightly clearer - but fundamentally, you're asking whether a list of CheckedNumber
values contains a particular int?
value. That makes no sense, as an int?
isn't a CheckedNumber
- it's like asking whether a List<Fruit>
contains a car.
If part or your CheckedNumber
struct is an int?
, you might want:
if (CheckedNumbers.Any(x => x.SomeProperty == TheNumber))
Alternatively, you might want:
if (CheckedNumbers.Contains(new CheckedNumber(TheNumber)))
Otherwise, you'll have to clarify the question further.