I’m pretty new at EF and Linq.
Please help me to convert this Method Syntax query to a Linq one.
Method Syntax to retrieve a single scalar variable from my DataBase using EF.
string myCategoryTitle = context.CmsCategories.SingleOrDefault(x => x.CategoryId == rowView.CategoryId).Title;
Title rappresent a propriety/field in my model.
Any idea how to do it using a Linq Syntax?
Thanks for your help!
I assume you mean using a query expression… in which case, you can’t. There’s no query expression syntax for
SingleOrDefault.You could use this though:
Note that this isn’t quite the same as the current code, which will throw a
NullReferenceExceptionifSingleOrDefault(...)returns null…