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

Commit 9817cf8

Browse files
committed
Added "include=" parameter to LoadSingleById method
1 parent 00043a5 commit 9817cf8

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/ServiceStack.OrmLite/OrmLiteReadApi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,9 @@ public static long LongScalar(this IDbConnection dbConn)
669669
/// Returns the first result with all its references loaded, using a primary key id. E.g:
670670
/// <para>db.LoadSingleById&lt;Person&gt;(1)</para>
671671
/// </summary>
672-
public static T LoadSingleById<T>(this IDbConnection dbConn, object idValue)
672+
public static T LoadSingleById<T>(this IDbConnection dbConn, object idValue, string[] include = null)
673673
{
674-
return dbConn.Exec(dbCmd => dbCmd.LoadSingleById<T>(idValue));
674+
return dbConn.Exec(dbCmd => dbCmd.LoadSingleById<T>(idValue, include));
675675
}
676676

677677
/// <summary>

src/ServiceStack.OrmLite/OrmLiteReadCommandExtensions.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ internal static long ToLong(object result)
878878
return (long)result;
879879
}
880880

881-
internal static T LoadSingleById<T>(this IDbCommand dbCmd, object value)
881+
internal static T LoadSingleById<T>(this IDbCommand dbCmd, object value, string[] include = null)
882882
{
883883
var row = dbCmd.SingleById<T>(value);
884884
if (row == null)
@@ -889,9 +889,21 @@ internal static T LoadSingleById<T>(this IDbCommand dbCmd, object value)
889889
return row;
890890
}
891891

892-
public static void LoadReferences<T>(this IDbCommand dbCmd, T instance)
892+
public static void LoadReferences<T>(this IDbCommand dbCmd, T instance, string[] include = null)
893893
{
894894
var loadRef = new LoadReferencesSync<T>(dbCmd, instance);
895+
var fieldDefs = loadRef.FieldDefs;
896+
897+
if (!include.IsEmpty())
898+
{
899+
// Check that any include values aren't reference fields of the specified type
900+
var fields = fieldDefs.Select(q => q.FieldName);
901+
var invalid = include.Except<string>(fields).ToList();
902+
if (invalid.Count > 0)
903+
throw new ArgumentException("Fields '{0}' are not Reference Properties of Type '{1}'".Fmt(invalid.Join("', '"), typeof(T).Name));
904+
905+
fieldDefs = fieldDefs.Where(fd => include.Contains(fd.FieldName)).ToList();
906+
}
895907

896908
foreach (var fieldDef in loadRef.FieldDefs)
897909
{

0 commit comments

Comments
 (0)