How to use Npoco FetchOneToMany?
Asked Answered
W

1

3

I am trying to use Npoco but running into some problems with FetchOneToMany

I have a sql statement that joins 2 tables together and I output all the columns.

   [TableName("TableA")]
    [PrimaryKey("Id")]
    public class TableA
    {
        public int Id { get; set; }
        public DateTime EffectiveDate { get; set; }
         public IList<TableB> TableBs { get; set; }
    }

    [TableName("TableB")]
    [PrimaryKey("TableBId")]
    public class TableB
    {
        public int TableBId { get; set; }
        public int SomeNumber { get; set; }
          public int Id { get; set; } // TableA id
    }


Func<TableA, object> func1 = (x) => x.Id;
            Func<TableB, object> func2 = (x) => x.Id;
  var test = RelationExtensions.FetchOneToMany<AdminFeeBandGroup, AdminFeeBand>(unitOfWork.Db, func1,func2,sql, 1,1,"10-18-2012","10-22-2012");

I am passing 4 parameters in my real query. I get a result back and TableBs property is filled and looks good. However EffectiveDate is not filled for some reason and is the default C# time.

What am I missing?

Edit

This is what I have as my query

SELECT     TableA.Id, TableA.EffectiveDate, TableB.TableBId, TableB.SomeNumber
FROM         TableA INNER JOIN
                      TableB ON TableA.Id = TableB.Id
WHERE     (TableA.EffectiveDate = @0)


Func<TableA, object> func1 = (x) => x.Id;
            Func<TableB, object> func2 = (x) => x.Id;
  var test = RelationExtensions.FetchOneToMany<AdminFeeBandGroup, AdminFeeBand>(unitOfWork.Db, func1,func2,sql, "10-18-2012");
Whitethroat answered 22/10, 2012 at 20:49 Comment(1)
I can't post the real Sql but I can make it look exactly the same.Whitethroat
P
3

This is how it should work.

var sql = "select a.*, b.* from tablea a 
    inner join tableb b on a.id = b.id 
    where EffectiveDate = @0"

List<TableA> results = 
    db.FetchOneToMany<TableA,TableB>(x=>x.Id, sql, new DateTime(2012,10,18))

You must ensure your columns are selected in the same order that the generic parameters are listed.

Problematic answered 23/10, 2012 at 0:23 Comment(3)
I am not sure what you mean by " ensure your columns are selected in the same order that the generic parameters are listed."Whitethroat
a.*, b.* is in the same order as <TableA,TableB> ...tableA columns come before B.Problematic
Yep that was the problem. On a side note what is the difference between the one you have in npoco and the other relationship extension you made?Whitethroat

© 2022 - 2024 — McMap. All rights reserved.