Skip to content
\n
[Table(\"book\")]\npublic class Book : BaseModel\n{\n    [PrimaryKey(\"id\")]\n    public int Id { get; set; }\n\n    [Column(\"title\")]\n    public string Title { get; set; } = \"\";\n\n    [Reference(typeof(Author), includeInQuery: true, useInnerJoin: false)]\n    public List<Author> Authors { get; set; } = default!;\n}
\n
[Table(\"recipe\")]\npublic class Recipe : BaseModel\n{\n    [PrimaryKey(\"id\")]\n    public int Id { get; set; }\n\n    [Column(\"name\")]\n    public string Name { get; set; } = \"\";\n\n    [Reference(typeof(Book), joinType: ReferenceAttribute.JoinType.Left, true)]\n    public Book? Book { get; set; }\n\n    [Column(\"page\")]\n    public int? Page { get; set; }\n}
\n

The Question

\n

I want to retrieve all the books by an author, I've tried some linq, filters and it never work... I wish I could do something simple as this:

\n
    public async Task<IEnumerable<Book>> GetBooksByAuthor(int authorId)\n    {\n        var result = await client\n            .From<Book>()\n            .Where(b => b.Authors.Contains(authorId)\n            .Get();\n    }
\n

Any clue to point me in the right direction?

","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"

You need to specify an inner join such as below. I also had a problem getting my head around the querying format, it doesn't work like linq as it does not translate it into a sql query like a traditional direct linq query to the SQL DB.

\n

The other (more expensive) alternative is to bring back all the books and then you can use linq to filter the \"Models\" collection as each Book item will have it's Author filled. But beware that each item in Models has a null 'BaseUrl' hence you can't use that item to call Update on it.

\n

Good luck

\n
await client\n            .From<Book>()\n              .Select(\"*, Author_info:Author!inner(*)\")\n              .Filter(\"Author.id\", Constants.Operator.Equals, authorId)\n              .Get();\n\t\t\n
","upvoteCount":2,"url":"https://github.com/supabase-community/supabase-csharp/discussions/178#discussioncomment-11811886"}}}

How to get data from a Many-To-Many relation #178

Answered by s3312345
mongeon asked this question in Q&A
Discussion options

You must be logged in to vote

You need to specify an inner join such as below. I also had a problem getting my head around the querying format, it doesn't work like linq as it does not translate it into a sql query like a traditional direct linq query to the SQL DB.

The other (more expensive) alternative is to bring back all the books and then you can use linq to filter the "Models" collection as each Book item will have it's Author filled. But beware that each item in Models has a null 'BaseUrl' hence you can't use that item to call Update on it.

Good luck

await client
            .From<Book>()
              .Select("*, Author_info:Author!inner(*)")
              .Filter("Author.id", Constants.Operator.Equals, author…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mongeon
Comment options

@baileyfrye1
Comment options

Answer selected by mongeon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants