0

I have for example two tables like:

CREATE TABLE [dbo].[Parent](
    [Id] [int] NOT NULL,
    [Name] [nvarchar](50) NOT NULL
) ON [PRIMARY]

GO

BEGIN TRANSACTION;
INSERT INTO [dbo].[Parent]([Id], [Name])
SELECT 1, N'First parent' UNION ALL
SELECT 2, N'Second parent' UNION ALL
SELECT 3, N'Parent with no childrens'
COMMIT;

CREATE TABLE [dbo].[Child](
    [Id] [int] NOT NULL,
    [ParentId] [int] NOT NULL,
    [Name] [nvarchar](50) NOT NULL
) ON [PRIMARY]

GO

BEGIN TRANSACTION;
INSERT INTO [dbo].[Child]([Id], [ParentId], [Name])
SELECT 1, 1, N'First child' UNION ALL
SELECT 2, 2, N'Second child'
COMMIT;

And then two c# classes like:

public class Parent
{
    public int Id { get; set; }
    public string Name { get; set; }
    public IList<Child> Children { get; set; }
}

public class Child
{
    public int Id { get; set; }
    public int ParentId { get; set; }
    public string Name { get; set; }
}

When I use Petapoco and relationextensions like (where db is a instance of Petapocos database object):

var parents = db.FetchOneToMany<Parent, Child>(p => p.Id, "SELECT * FROM Parent AS p LEFT JOIN Child AS c ON p.Id = c.ParentId");

I get the expected result of parents, three items in my list with correct ids but NOT with correct children. The first two are correct with their connected children, "first child" and "second child" BUT the third parent which has no children in the database get a default instance of a child. With that I mean the third parent has a collection of children with count 1. And that list contains one child which has only default values. Id = 0, Name = NULL, ParentId = 0. This is not what I want. I would like that collection to not contain any children. The alternatives I see it is that collection to be null or an instance of IList but with no items.

What am I doing wrong and how could I correct it?

Milo
  • 3,365
  • 9
  • 30
  • 44
John
  • 2,043
  • 5
  • 28
  • 49

1 Answers1

2

There should be an overload which takes a second lambda. If there isn't then you should be able to use this version of it. https://github.com/schotime/NPoco/blob/dev2.0/src/NPoco/RelationExtensions.cs

You may have to change the IDatabase to Database however it should work otherwise.

Otherwise call:

db.FetchOneToMany<Parent, Child>(p => p.Id, c => c.Id, "your sql here");

The second lambda is the primary key of the child table.

Schotime
  • 15,707
  • 10
  • 46
  • 75
  • Thank you. Exactly what is NPoco? Is it a microORM itself or is it an extension which works on top of PetaPoco? Are you the author of PetaPoco as well? – John Jan 02 '13 at 07:57
  • I believe NPoco is PetaPoco with Schotimes additions rolled in. Schotime is the author of NPoco. I am using it in an e-commerce project at the moment and its been good to work with. – David McLean Jan 03 '13 at 23:36
  • That is correct. NPoco is the rollup of my changes to PetaPoco. More info can be found here: https://github.com/schotime/NPoco/wiki and its available on NuGet. – Schotime Jan 05 '13 at 01:11
  • Does NPoco follow PetaPocos versions? If PetaPoco releases a new version tomorrow with features that I am interested in, would I be able to take benefit from them if I go with NPoco instead of PetaPoco? Or do I have to wait for NPoco to integrate these changes into the next release of NPoco? – John Jan 08 '13 at 13:41
  • NPoco at the moment covers everything in PetaPoco except the TT files. If a good feature is implemented in PetaPoco it will be ported to NPoco however, the main branch of PetaPoco is not currently under active dev, unlike NPoco, so you have a better chance of staying active with NPoco, hence why I created it. – Schotime Jan 09 '13 at 00:57
  • Great. Thank you very much. I am just in the early start of new project here and we took the decision to go with a microORM instead of a regular ORM-mapper and our choice landed on PetaPoco. But I will probably take a closer look at NPoco and maybe switch PetaPoco with that. One question though that I am especially interested in. Multiple result set, why is that feature limited to only two result set returning a tuple? Is there a way to return a list of lists instead of a tuple of lists? – John Jan 09 '13 at 06:37
  • It is not limited to 2, but 4. In theory it could be implemented as a list of lists however I chose not to, so that each item could be type safe. Otherwise the outer list would be a `List>` where T could be anything and not known at compile time. – Schotime Jan 11 '13 at 13:14