jhhoff02 jhhoff02 - 25 days ago 4
C# Question

Array.Find<List<Status>> Error: List<Status> does not contain a definition for 'StatusID'

I tried finding a similar issue to this one.. I am wondering if anyone can point out the flaw with my code. I have a

List
called
statuses
of type
Status
class, the class looks like this:

public class Status
{
public int StatusID { get; set; }
public string StatusDescr { get; set; }
public string StatusType { get; set; }
}


I retrieve an array/list of records from SQL and want to find the StatusDescr using a known StatusID:

List<Status> statuses = Status_Get(inputAction, dataDef);
var inputStatusID = fooData["StatusID"];
string statusDescr = Array.Find<List<Status>>(statuses, item => item.StatusID == inputStatusID).StatusDescr;


but I get an error on
item.StatusID
in the 3rd line saying:
'List<Status> does not contain a definition for 'StatusID' and no extension method 'StatusID' accepting a first argument of type 'List<Status>' could be found'


Obviously this is because StatusID exists on type Status, not
List<Status>
.
If I change it to
Array.Find<Status>
, I get an error on
statuses
saying:
`Cannot convert from '...List<...DataTypes.View.Status>' to '...DataTypes.View.Status[]'.

Basically I don't care if
statuses
is a List, array, dynamic, etc.. I just want to retrieve the StatusDescr from the Status object with a matching StatusID.
Any ideas on data types? Should I use something besides
Array.Find
?

Edit:
If I take out the data type after
Find
like this:

Array.Find(statuses, item => item.StatusID == inputStatusID).StatusDescr;


I get an error on
Find
saying: The type arguments for method 'Array.Find(T[], Predicate)' cannot be inferred from the usage. Try specifying the type arguments explicitly

Answer
var result = statuses.FirstOrDefault(x=> x.StatusID == inputStatusID);

string statusDescr = result?.StatusDescr;

You need something like this.

Here check dotnet fiddle

Comments