LINQ expression with FirstOrDefault


I’m wanting to make a variable which will hold the Rowcode value of the row in the roster which satisfies a certain condition. When I try to do this using a LINQ expresion. e.g. ROSTERNAME.FirstOrDefualt - I receive an error "cannot implicitly convert type… "
I’m using a long integer for the variable type because I thought this would return the row number… Just to note the rest of my syntax is fine because if I replace ‘FirstOrDefault’ with ‘Count’ then it’s working just fine.

Anyone have any experience getting a LINQ FirstOrDefaul to work correctly?



FirstOrDefault will return an object corresponding to a line (an item) in the roster. Not an index. But, e.g. the person with all his characteristics like age, gender, education. You can pick that attribute, or take the code or index of that item.

VariableX = PERSONS.FirstOrDefault(x=>x.Age>15).@rowcode

VariableX will be null if there is no such person (above 15 y.o. in this case).

Count() worked because you can count these objects.

Best, Sergiy

1 Like