Created
May 20, 2019 13:48
-
-
Save wwwlicious/1c0b692464ca7fa76c327b68273a889a to your computer and use it in GitHub Desktop.
populating child object from anonymous
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace UseCase.Tests | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Globalization; | |
using System.Linq; | |
using System.Runtime.Serialization; | |
using System.Text; | |
using ServiceStack.Data; | |
using ServiceStack.DataAnnotations; | |
using ServiceStack.OrmLite; | |
using Xunit; | |
public class TestCase | |
{ | |
private readonly IDbConnectionFactory dbFactory; | |
public TestCase() | |
{ | |
dbFactory = new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider); | |
using (var db = dbFactory.OpenDbConnection()) | |
{ | |
db.CreateTableIfNotExists<NDMAS>(); | |
db.CreateTableIfNotExists<PLSUPP>(); | |
db.Insert( | |
new NDMAS | |
{ | |
NDM_NDCODE = "123", | |
NDM_ADDR1 = "Add1", | |
NDM_ADDR2 = "Add2", | |
NDM_ADDR3 = "Add3", | |
NDM_ADDR4 = "Add4", | |
NDM_ADDR5 = "ADD%", | |
NDM_POSTCODE = "POST123", | |
NDM_AREA = "AREA", | |
NDM_NAME = "name1", | |
NDM_EMAIL = "EMAIL", | |
NDM_ADDTYPE = "S", | |
NDM_TELEPHONE = "123", | |
NDM_TELEX = "1", | |
NDM_DESC = "A", | |
NDM_COUNTRY = "UK" | |
}); | |
db.Insert(new PLSUPP | |
{ | |
PLSUP_NDCODE = "123", | |
PLSUP_SUPPLIER = "SUPP1", | |
PLSUP_BRANCH = "A", | |
}); | |
} | |
} | |
[Fact] | |
public void CanPopulateAddressTest() | |
{ | |
var s = TestList(); | |
Assert.NotEmpty(s); | |
var add = s.First(); | |
Assert.NotNull(add.Address); | |
} | |
public List<Supplier> TestList() | |
{ | |
using (var db = dbFactory.OpenDbConnection()) | |
{ | |
return db.Select<Supplier>( | |
db.From<PLSUPP>().Join<NDMAS>((plsupp, ndmas) => plsupp.PLSUP_NDCODE == ndmas.NDM_NDCODE) | |
.Select<PLSUPP, NDMAS>( | |
(plsupp, ndmas) => new | |
{ | |
Code = plsupp.PLSUP_SUPPLIER, | |
Address = new { | |
Address1 = ndmas.NDM_ADDR1, | |
Address2 = ndmas.NDM_ADDR2, | |
Address3 = ndmas.NDM_ADDR3, | |
Address4 = ndmas.NDM_ADDR4, | |
Address5 = ndmas.NDM_ADDR5, | |
Postcode = ndmas.NDM_POSTCODE, | |
Country = ndmas.NDM_COUNTRY, | |
}, | |
AddressType = ndmas.NDM_ADDTYPE, | |
Area = ndmas.NDM_AREA, | |
Description = ndmas.NDM_DESC, | |
Email = ndmas.NDM_EMAIL, | |
LastDate = ndmas.NDM_LASTDATE, | |
LastTime = ndmas.NDM_LASTTIME, | |
Name = ndmas.NDM_NAME, | |
Telephone = ndmas.NDM_TELEPHONE, | |
Telex = ndmas.NDM_TELEX | |
})); | |
} | |
} | |
} | |
[DataContract] | |
public class Supplier | |
{ | |
[DataMember] | |
public string Code { get; set; } | |
[DataMember] | |
public string Name { get; set; } | |
[DataMember] | |
[Reference] | |
public Address Address { get; set; } | |
[DataMember] | |
public string FormattedAddress | |
{ | |
get => Address.ToString("<br />"); | |
set => Address.ToString("<br />"); | |
} | |
[DataMember] | |
public string Telephone { get; set; } | |
[DataMember] | |
public string Telex { get; set; } | |
[DataMember] | |
public DateTime LastDate { get; set; } | |
[DataMember] | |
public string AddressType { get; set; } | |
[DataMember] | |
public decimal LastTime { get; set; } | |
[DataMember] | |
public string Email { get; set; } | |
[DataMember] | |
public string Area { get; set; } | |
[DataMember] | |
public string Description { get; set; } | |
[DataMember] | |
public string TrimmedCode { get; set; } | |
[DataMember] | |
public char OnStop { get; set; } | |
} | |
public class Address | |
{ | |
/// <summary> | |
/// Gets or sets Line 1 of the address. | |
/// </summary> | |
/// <value>The address1.</value> | |
[StringLength(30)] | |
[Required] | |
public string Address1 { get; set; } | |
/// <summary> | |
/// Gets or sets Line 2 of the address. | |
/// </summary> | |
/// <value>The address2.</value> | |
[StringLength(30)] | |
public string Address2 { get; set; } | |
/// <summary> | |
/// Gets or sets Line 3 of the address. | |
/// </summary> | |
[StringLength(30)] | |
public string Address3 { get; set; } | |
/// <summary> | |
/// Gets or sets Line 4 of the address. | |
/// </summary> | |
/// <value>The address4.</value> | |
[StringLength(30)] | |
public string Address4 { get; set; } | |
/// <summary> | |
/// Gets or sets Line 5 of the address. | |
/// </summary> | |
/// <value>The address5.</value> | |
[StringLength(30)] | |
public string Address5 { get; set; } | |
/// <summary> | |
/// Gets or sets Country of the address. | |
/// </summary> | |
/// <value>The country.</value> | |
[StringLength(2)] | |
[Required] | |
public string Country { get; set; } | |
/// <summary> | |
/// Gets or sets Postcode of the address. | |
/// </summary> | |
/// <value>The postcode.</value> | |
[StringLength(10)] | |
[Required] | |
public string Postcode { get; set; } | |
public new string ToString() | |
{ | |
return ToString(","); | |
} | |
public string ToString(object separator) | |
{ | |
var sb = new StringBuilder(Address1); | |
if (!string.IsNullOrEmpty(Address2)) | |
{ | |
sb.AppendFormat(CultureInfo.CurrentCulture, "{0} {1}", separator, Address2); | |
} | |
if (!string.IsNullOrEmpty(Address3)) | |
{ | |
sb.AppendFormat(CultureInfo.CurrentCulture, "{0} {1}", separator, Address3); | |
} | |
if (!string.IsNullOrEmpty(Address4)) | |
{ | |
sb.AppendFormat(CultureInfo.CurrentCulture, "{0} {1}", separator, Address4); | |
} | |
if (!string.IsNullOrEmpty(Address5)) | |
{ | |
sb.AppendFormat(CultureInfo.CurrentCulture, "{0} {1}", separator, Address5); | |
} | |
if (!string.IsNullOrEmpty(Country)) | |
{ | |
sb.AppendFormat(CultureInfo.CurrentCulture, "{0} {1}", separator, Country); | |
} | |
sb.AppendFormat(CultureInfo.CurrentCulture, "{0} {1}", separator, Postcode); | |
return sb.ToString().TrimEnd(','); | |
} | |
} | |
public class PLSUPP | |
{ | |
[StringLength(10)] | |
[Required] | |
public string PLSUP_SUPPLIER { get; set; } | |
[StringLength(4)] | |
[Required] | |
public string PLSUP_BRANCH { get; set; } | |
[StringLength(10)] | |
[Required] | |
public string PLSUP_NDCODE { get; set; } | |
[StringLength(10)] | |
public string PLSUP_PAYEE { get; set; } | |
public decimal PLSUP_LIMIT { get; set; } | |
public decimal PLSUP_BALANCE { get; set; } | |
public char PLSUP_TYPE { get; set; } | |
public char PLSUP_REMIT { get; set; } | |
[StringLength(4)] | |
public string PLSUP_TERMCODE { get; set; } | |
public DateTime PLSUP_DATEOPEN { get; set; } | |
public DateTime PLSUP_LASTPURCH { get; set; } | |
public DateTime PLSUP_LASTPAY { get; set; } | |
[StringLength(2)] | |
public string PLSUP_STOP { get; set; } | |
[StringLength(4)] | |
public string PLSUP_CURRENCY { get; set; } | |
[StringLength(15)] | |
public string PLSUP_DEFGL { get; set; } | |
[StringLength(10)] | |
public string PLSUP_ALPHACODE { get; set; } | |
public char PLSUP_PRINTPO { get; set; } | |
[StringLength(2)] | |
public string PLSUP_COUNTRY { get; set; } | |
[StringLength(15)] | |
public string PLSUP_TXREGNO { get; set; } | |
public char PLSUP_ECFREIGHT { get; set; } | |
public char PLSUP_TRANSPCOD { get; set; } | |
public decimal PLSUP_CVRATE { get; set; } | |
public decimal PLSUP_QUOTVAR { get; set; } | |
public decimal PLSUP_CLAIM { get; set; } | |
[StringLength(4)] | |
public string PLSUP_PAYRUN { get; set; } | |
[StringLength(4)] | |
public string PLSUP_DIV { get; set; } | |
public decimal PLSUP_NOREMIT { get; set; } | |
[StringLength(10)] | |
public string PLSUP_PDTRAN { get; set; } | |
[StringLength(10)] | |
public string PLSUP_CDTRAN { get; set; } | |
public char PLSUP_EXPFORM { get; set; } | |
public char PLSUP_IMPFORM { get; set; } | |
public decimal PLSUP_SAFEBUFF { get; set; } | |
public char PLSUP_SDTYPE { get; set; } | |
[StringLength(20)] | |
public string PLSUP_COREGNI { get; set; } | |
public char PLSUP_REMDEL { get; set; } | |
[StringLength(4)] | |
public string PLSUP_MNGRID { get; set; } | |
public char PLSUP_SDEXP { get; set; } | |
public char PLSUP_SDIMP { get; set; } | |
public decimal PLSUP_SDMARGIN { get; set; } | |
public char PLSUP_RATING { get; set; } | |
public char PLSUP_DATECHK { get; set; } | |
[StringLength(3)] | |
public string PLSUP_DELTERM { get; set; } | |
public char PLSUP_DEALS { get; set; } | |
public char PLSUP_SPDEDI { get; set; } | |
public decimal PLSUP_EDIREP1 { get; set; } | |
[StringLength(4)] | |
public string PLSUP_BUYER { get; set; } | |
public char PLSUP_PCTYPE { get; set; } | |
public char PLSUP_VAT { get; set; } | |
public char PLSUP_MASTER { get; set; } | |
[StringLength(4)] | |
public string PLSUP_SHIPMETH { get; set; } | |
public char PLSUP_PPTYPE { get; set; } | |
public decimal PLSUP_PPDAYS { get; set; } | |
public decimal PLSUP_SDDAYS { get; set; } | |
public char PLSUP_DOCREG { get; set; } | |
[StringLength(4)] | |
public string PLSUP_BARLYT { get; set; } | |
[StringLength(5)] | |
public string PLSUP_PRICELOC { get; set; } | |
public decimal PLSUP_PLUPPER { get; set; } | |
[StringLength(2)] | |
public string PLSUP_LANG { get; set; } | |
[StringLength(15)] | |
public string PLSUP_NETSUSP { get; set; } | |
public decimal PLSUP_NETTBILL { get; set; } | |
public decimal PLSUP_MINCOS { get; set; } | |
public decimal PLSUP_FIXCOS { get; set; } | |
public decimal PLSUP_SRETCOST { get; set; } | |
public decimal PLSUP_CRTCOST { get; set; } | |
public decimal PLSUP_SHLDCOST { get; set; } | |
[StringLength(4)] | |
public string PLSUP_TRCURR { get; set; } | |
public char PLSUP_ACANAL { get; set; } | |
[StringLength(13)] | |
public string PLSUP_SPDREF { get; set; } | |
public decimal PLSUP_ACKDAYS { get; set; } | |
public decimal PLSUP_TRANSIT { get; set; } | |
public decimal PLSUP_PRCOPIES { get; set; } | |
public char PLSUP_EDIINV { get; set; } | |
public char PLSUP_EDIADD { get; set; } | |
public decimal PLSUP_EDIMAXADD { get; set; } | |
public decimal PLSUP_VATVAR { get; set; } | |
public decimal PLSUP_ORDLINVAR { get; set; } | |
[StringLength(4)] | |
public string PLSUP_RTSLOGIN { get; set; } | |
public decimal PLSUP_RTSDAYS { get; set; } | |
[StringLength(10)] | |
public string PLSUP_RTS { get; set; } | |
[StringLength(2)] | |
public string PLSUP_CODCODE { get; set; } | |
public decimal PLSUP_MANDINT { get; set; } | |
public char PLSUP_DATEMETH { get; set; } | |
public char PLSUP_FRCST { get; set; } | |
[StringLength(15)] | |
public string PLSUP_POGLACC { get; set; } | |
public char PLSUP_VATONLY { get; set; } | |
public char PLSUP_POSTOP { get; set; } | |
[StringLength(4)] | |
public string PLSUP_CODE1 { get; set; } | |
[StringLength(4)] | |
public string PLSUP_CODE2 { get; set; } | |
[StringLength(4)] | |
public string PLSUP_CODE3 { get; set; } | |
[StringLength(4)] | |
public string PLSUP_CODE4 { get; set; } | |
public char PLSUP_EUREQ { get; set; } | |
public char PLSUP_TRCODE { get; set; } | |
[StringLength(4)] | |
public string PLSUP_AREA { get; set; } | |
[StringLength(4)] | |
public string PLSUP_VANAREA { get; set; } | |
[StringLength(4)] | |
public string PLSUP_SERLEV { get; set; } | |
public char PLSUP_IMPQFORM { get; set; } | |
public char PLSUP_QUOTPRINT { get; set; } | |
[StringLength(8)] | |
public string PLSUP_QPARA { get; set; } | |
public DateTime PLSUP_LASTNBI { get; set; } | |
[StringLength(20)] | |
public string PLSUP_SUPPREF { get; set; } | |
public char PLSUP_VATDEBIT { get; set; } | |
public decimal? PLSUP_SLADAYS { get; set; } | |
public decimal? PLSUP_PPER { get; set; } | |
[StringLength(10)] | |
public string PLSUP_SUBCON { get; set; } | |
public char? PLSUP_SUBPOST { get; set; } | |
[StringLength(10)] | |
public string PLSUP_EMP { get; set; } | |
[StringLength(15)] | |
public string PLSUP_REB1ACC { get; set; } | |
[StringLength(15)] | |
public string PLSUP_REB2ACC { get; set; } | |
[StringLength(13)] | |
public string PLSUP_GLN { get; set; } | |
public char? PLSUP_POROUTE { get; set; } | |
[StringLength(15)] | |
public string PLSUP_REB3ACC { get; set; } | |
public decimal? PLSUP_MINORD { get; set; } | |
public char? PLSUP_SUPPROD { get; set; } | |
[StringLength(35)] | |
public string PLSUP_SUPCUSNO { get; set; } | |
public char? PLSUP_REGRULE { get; set; } | |
public char? PLSUP_EDICLAIM { get; set; } | |
public char? PLSUP_BS5750 { get; set; } | |
public DateTime? PLSUP_EXPBS5750 { get; set; } | |
[StringLength(60)] | |
public string PLSUP_COMMENT { get; set; } | |
[StringLength(40)] | |
public string PLSUP_SCOPE { get; set; } | |
public char? PLSUP_ROHS { get; set; } | |
public char? PLSUP_APPRSTAT { get; set; } | |
[StringLength(4)] | |
public string PLSUP_OLDCURR { get; set; } | |
public decimal? PLSUP_CONVRATE { get; set; } | |
public DateTime? PLSUP_CONVDATE { get; set; } | |
public decimal? PLSUP_NCONVRATE { get; set; } | |
public decimal? PLSUP_OCONVRATE { get; set; } | |
} | |
public class NDMAS | |
{ | |
[StringLength(10)] | |
[Required] | |
public string NDM_NDCODE { get; set; } | |
[StringLength(40)] | |
[Required] | |
public string NDM_NAME { get; set; } | |
[StringLength(30)] | |
[Required] | |
public string NDM_ADDR1 { get; set; } | |
[StringLength(30)] | |
[Required] | |
public string NDM_ADDR2 { get; set; } | |
[StringLength(30)] | |
[Required] | |
public string NDM_ADDR3 { get; set; } | |
[StringLength(30)] | |
[Required] | |
public string NDM_ADDR4 { get; set; } | |
[StringLength(30)] | |
[Required] | |
public string NDM_ADDR5 { get; set; } | |
[StringLength(10)] | |
[Required] | |
public string NDM_POSTCODE { get; set; } | |
[StringLength(20)] | |
[Required] | |
public string NDM_TELEPHONE { get; set; } | |
[StringLength(20)] | |
[Required] | |
public string NDM_TELEX { get; set; } | |
[Required] | |
public DateTime NDM_LASTDATE { get; set; } | |
[StringLength(10)] | |
[Required] | |
public string NDM_ADDTYPE { get; set; } | |
[Required] | |
public decimal NDM_LASTTIME { get; set; } | |
[StringLength(60)] | |
[Required] | |
public string NDM_EMAIL { get; set; } | |
[StringLength(4)] | |
[Required] | |
public string NDM_AREA { get; set; } | |
[StringLength(40)] | |
[Required] | |
public string NDM_DESC { get; set; } | |
[StringLength(2)] | |
[Required] | |
public string NDM_COUNTRY { get; set; } | |
[StringLength(4)] | |
public string NDM_ADCOD1 { get; set; } | |
[StringLength(4)] | |
public string NDM_ADCOD2 { get; set; } | |
public char? NDM_ADCHA1 { get; set; } | |
public char? NDM_ADCHA2 { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment