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

Commit 239bb63

Browse files
committed
Change LoadSelect to allow specifying empty include string array to load no references
1 parent 42b0a65 commit 239bb63

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/ServiceStack.OrmLite/OrmLiteReadCommandExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -898,15 +898,15 @@ public static void LoadReferences<T>(this IDbCommand dbCmd, T instance, string[]
898898
var loadRef = new LoadReferencesSync<T>(dbCmd, instance);
899899
var fieldDefs = loadRef.FieldDefs;
900900

901-
if (!include.IsEmpty())
901+
if (include != null)
902902
{
903903
// Check that any include values aren't reference fields of the specified type
904904
var fields = fieldDefs.Select(q => q.FieldName);
905-
var invalid = include.Except<string>(fields).ToList();
905+
var invalid = include.Except(fields).ToList();
906906
if (invalid.Count > 0)
907907
throw new ArgumentException("Fields '{0}' are not Reference Properties of Type '{1}'".Fmt(invalid.Join("', '"), typeof(T).Name));
908908

909-
fieldDefs = fieldDefs.Where(fd => include.Contains(fd.FieldName)).ToList();
909+
fieldDefs = fieldDefs.Where(f => include.Contains(f.FieldName)).ToList();
910910
}
911911

912912
foreach (var fieldDef in fieldDefs)
@@ -929,15 +929,15 @@ internal static List<Into> LoadListWithReferences<Into, From>(this IDbCommand db
929929
var loadList = new LoadListSync<Into, From>(dbCmd, expr);
930930

931931
var fieldDefs = loadList.FieldDefs;
932-
if (!include.IsEmpty())
932+
if (include != null)
933933
{
934934
// Check that any include values aren't reference fields of the specified From type
935935
var fields = fieldDefs.Select(q => q.FieldName);
936-
var invalid = include.Except<string>(fields).ToList();
936+
var invalid = include.Except(fields).ToList();
937937
if (invalid.Count > 0)
938938
throw new ArgumentException("Fields '{0}' are not Reference Properties of Type '{1}'".Fmt(invalid.Join("', '"), typeof(From).Name));
939939

940-
fieldDefs = loadList.FieldDefs.Where(fd => include.Contains(fd.FieldName)).ToList();
940+
fieldDefs = loadList.FieldDefs.Where(f => include.Contains(f.FieldName)).ToList();
941941
}
942942

943943
foreach (var fieldDef in fieldDefs)

tests/ServiceStack.OrmLite.Tests/LoadReferencesTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,14 @@ public void Can_load_only_included_references()
667667
Assert.That(dbCustomer.Orders, Is.Null);
668668
Assert.That(dbCustomer.PrimaryAddress, Is.Not.Null);
669669

670+
dbCustomers = db.LoadSelect<Customer>(q => q.Id == customer.Id, include: new string[0]);
671+
Assert.That(dbCustomers.All(x => x.Orders == null));
672+
Assert.That(dbCustomers.All(x => x.PrimaryAddress == null));
673+
674+
dbCustomer = db.LoadSingleById<Customer>(customer.Id, include: new string[0]);
675+
Assert.That(dbCustomer.Name, Is.EqualTo("Customer 1"));
676+
Assert.That(dbCustomer.Orders, Is.Null);
677+
Assert.That(dbCustomer.PrimaryAddress, Is.Null);
670678

671679
// Invalid field name
672680
try

0 commit comments

Comments
 (0)