I am trying to get the string value "Admin" from a linq query, but when I do:
string oldModule = dc.Modules
.Where(x => x.Id == um.ModuleId)
.Select(s => new {s.ModuleName})
.FirstOrDefault().ToString();
It returns { ModuleName = Admin } in the oldModule variable instead of just Admin.
That's because you've introduced an anonymous type into the projection. Try this:
string oldModule = dc.Modules
.Where(x => x.Id == um.ModuleId)
.Select(s => s.ModuleName)
.FirstOrDefault();
I removed the ToString call as well, which would have thrown a NullReferenceException if the Where clause hadn't matched anything.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With