Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 775e7c2

Browse files
committed
Add overload for including reference types using typed expression
1 parent 60db99b commit 775e7c2

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/ServiceStack.OrmLite/OrmLiteReadApi.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Data;
5+
using System.Linq;
56
using System.Linq.Expressions;
67

78
namespace ServiceStack.OrmLite
@@ -667,13 +668,22 @@ public static long LongScalar(this IDbConnection dbConn)
667668

668669
/// <summary>
669670
/// Returns the first result with all its references loaded, using a primary key id. E.g:
670-
/// <para>db.LoadSingleById&lt;Person&gt;(1)</para>
671+
/// <para>db.LoadSingleById&lt;Person&gt;(1, include = new[]{ "Address" })</para>
671672
/// </summary>
672673
public static T LoadSingleById<T>(this IDbConnection dbConn, object idValue, string[] include = null)
673674
{
674675
return dbConn.Exec(dbCmd => dbCmd.LoadSingleById<T>(idValue, include));
675676
}
676677

678+
/// <summary>
679+
/// Returns the first result with all its references loaded, using a primary key id. E.g:
680+
/// <para>db.LoadSingleById&lt;Person&gt;(1, include = x => new{ x.Address })</para>
681+
/// </summary>
682+
public static T LoadSingleById<T>(this IDbConnection dbConn, object idValue, Func<T,object> include)
683+
{
684+
return dbConn.Exec(dbCmd => dbCmd.LoadSingleById<T>(idValue, include(typeof(T).CreateInstance<T>()).GetType().AllAnonFields() ));
685+
}
686+
677687
/// <summary>
678688
/// Loads all the related references onto the instance. E.g:
679689
/// <para>db.LoadReferences(customer)</para>

src/ServiceStack.OrmLite/OrmLiteUtils.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.Collections;
1414
using System.Collections.Generic;
1515
using System.Data;
16+
using System.Linq;
1617
using System.Text;
1718
using ServiceStack.Logging;
1819
using ServiceStack.Text;
@@ -659,5 +660,10 @@ public static List<string> ParseTokens(this string expr)
659660

660661
return to;
661662
}
663+
664+
public static string[] AllAnonFields(this Type type)
665+
{
666+
return type.GetPublicProperties().Select(x => x.Name).ToArray();
667+
}
662668
}
663669
}

tests/ServiceStack.OrmLite.Tests/LoadReferencesTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,11 @@ public void Can_load_only_included_references()
659659
// Test LoadSingleById
660660
var dbCustomer = db.LoadSingleById<Customer>(customer.Id, include: new[] { "PrimaryAddress" });
661661
Assert.That(dbCustomer.Name, Is.EqualTo("Customer 1"));
662+
Assert.That(dbCustomer.Orders, Is.Null);
663+
Assert.That(dbCustomer.PrimaryAddress, Is.Not.Null);
662664

665+
dbCustomer = db.LoadSingleById<Customer>(customer.Id, include: x => new { x.PrimaryAddress });
666+
Assert.That(dbCustomer.Name, Is.EqualTo("Customer 1"));
663667
Assert.That(dbCustomer.Orders, Is.Null);
664668
Assert.That(dbCustomer.PrimaryAddress, Is.Not.Null);
665669

0 commit comments

Comments
 (0)