Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator '==' cannot be applied to operands 'method group' or 'string'

I have the following code:

  <span>@Model.LicenseHolder.LegalPerson.ContactDetails.Select(x => x.Name == "Fish")</span>

When I run this, I get the error:

Operator '==' cannot be applied to operands 'method group' or 'string'

I don't understand why I get this.

Here you can see a picture of ContactDetails: 'enter image description here

I want to access the ContactDataType property and compare the Name-property that Is Inside ContactDataType, but I don't know how do It. So basically, I want this: @Model.LicenseHolder.LegalPerson.ContactDetails.ContactDataType.Select(x => x.Name == "PrimaryPhone")

like image 361
Bryan Avatar asked Jan 06 '23 11:01

Bryan


2 Answers

You need to apply it to your Where not Select function:

<span>@Model.LicenseHolder.LegalPerson.ContactDetails.Where(x => x.Name == "Fish").FirstOrDefault()</span>

Or even better:

<span>@Model.LicenseHolder.LegalPerson.ContactDetails.FirstOrDefault(x => x.Name == "Fish")</span>
like image 53
Oluwafemi Avatar answered Jan 08 '23 00:01

Oluwafemi


The problem is, that you try to access the Name member which is probably a method and not a property.
On your screenshot we can see all available properties: ContactDataType, DebuggerDisplay, Detail, Id and PersonId. There is no Name. If it is a property you must add () to Name to execute it:

@Model.LicenseHolder.LegalPerson.ContactDetails.Select(x => x.Name() == "Fish")

This will return a IEnumerable<Boolean> but probably you want something else.
Maybe this?

@Model.LicenseHolder.LegalPerson.ContactDetails
      .FirstOrDefault(x => x.Name() == "Fish")
      .ContactDataType 

This will return the first ContactDetails ContactDataType which Name() equals "Fish".

like image 25
Koopakiller Avatar answered Jan 08 '23 01:01

Koopakiller