c# - Using Equals with LINQ on an IEnumerable -


this code :

var allsubjectsforastudent =    getallsubjects<subject>(studentid); 

returns

ienumerable<subject>  

and can see bunch of subjects returned in debugger.

want check particular subject doing case insensitive comparison.

this code have:

var studentsubjects = allsubjectsforastudent.where(s => s.name.equals(subjectname, stringcomparison.currentcultureignorecase)); 

'subjectname' parameter method recieve.

when line executes 'object not set instance of object' error.

so want case insensitive search , return first item when there more 1 , return empty collection when there none.

any clues?

edit 1

the answers suggest there can entry in first collection might have 'null'. while observation true program makes sure 'subject name' can not null value. hope helps.

thanks in advance.

you try:

var studentsubjects = allsubjectsforastudent.where(s => !string.isnullorwhitespace(s.name) && s.name.toupperinvariant() == subjectname.toupperinvariant()).firstordefault(); 

this either set studentsubjects null or first instance in ienumerable matches.

you're getting nullreferenceexception when call

s.name.equals(subjectname) 

for object s.name null.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

node.js - Using Node without global install -

php - CakePHP HttpSockets send array of paramms -