Access database field in one to many relationship in asp net core
In my example I have Polls and PollOptions tables, I can access PollOptions.Poll table but not the other way around
5 Replies
Please share the relevant classes, otherwise there's not much to say apart from "Make sure the
Poll
class has a visible PollOptions
collection"Well, you can access the properties that you have
Poll.PollOptions
is a collection
It has the properties of a collectionYou can't access
PollOptions.Poll
because your collection has many polls
So if you want to access it, you have to ask yourself in which way
For example, if you want to return all the polls that exist in the poll options, you can use LINQ.
LINQ has handy shorthands that make it easy to access collections amongst other things
In this case you use Select
to select an inner property/field/method in a collection
You already used LINQ's Where
method btw, so it's similar
x => x.PollOptions.Select(y => y.Poll)
Yeah, I tried doing something really stupid there, thanks 😄