Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I am working on an MVC 3 project .. just updating a few things here and there..

Is it possible to Use FirstOrDefault followed by Where clause? at the moment it does not let me. Is there any possible solution to go around this?

Example ...

C#
var car = carContext.Vehicles.FirstOrDefault(x =>x.Reg = reg).Where(x =>x.manufacturere == "BMW")


is there a way around this??
Posted
Comments
[no name] 29-Oct-15 6:54am    
Can you please why you want this ? what is your requirement? you can do it by other mean but for that i have to understand your requirement.
1Future 29-Oct-15 7:02am    
Hi .. I just need ti return a single result to my view rather than return a list... so that's i would like to use FirstOrDefault ..
1Future 29-Oct-15 7:07am    
Is there a way we could stay in touch?? i just read your profile and you seem to know the technologies i'm currently learning i.e angularjs.. maybe i could ask u a question just incase i got stuck!

var car = carContext.Vehicles.FirstOrDefault(x =>x.Reg == reg && x.manufacturere == "BMW")


That will select the first where the reg and manufacturer match the parameters supplied.
 
Share this answer
 
Comments
1Future 29-Oct-15 7:14am    
actually is even a better idea.. Thank you sir!
No.
FirstOrDefault returns a single value, not a collection - so Where (which operates only on collections) has nothing to work with.
You can use it the other way round though: Where(...).FirstOrDefault()
 
Share this answer
 
Comments
1Future 29-Oct-15 7:03am    
ahh I see Thank you
OriginalGriff 29-Oct-15 7:18am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900