Ef Core Lambda All records from parent and count of child

Viewed 483

How can I retrieve all records from parent and count of child entries along with it using lambda and ef core (C#)?

For example : List all blog posts and total comments for each post.

Table Post

Id, title

1, "Hello World"

2, "Hello Stackoverflow"

3, "Hello C#"

Table Comments

Id, PostId, Comment

1, 1, "Small world :-)"

2, 1, "Hello world too",

3, 2, "Nice website",

4, 3, "the best language"

Desired output

Id, title, comments count

1, "Hello World", 2

2, "Hello Stackoverflow", 1

3, "Hello C#", 1

How can i achieve the same in lambda with 1 single call to database?

Please note I do not want to load include comments upfront? I just need all post and count of comments. Thanks! :-)

0 Answers
Related