なぜあなたはこれを行うことができません:
/////select をテストするための SQL コード
DECLARE @People TABLE (Id INT, Name VARCHAR(50))
INSERT INTO @People (Id,Name)
SELECT 1,' John Smith' UNION ALL
SELECT 2,'Albert Lee' UNION ALL
SELECT 3,'Christina Wetherbe' UNION ALL
SELECT 4,'Alice Cany' UNION ALL
SELECT 5,'Jim Blabery' UNION ALL
SELECT 6,'Octaviose Mayflower' UNION ALL
SELECT 7,'Sandra Lee M' UNION ALL
SELECT 8,'Some test user' UNION ALL
SELECT 9,'Some test user 2' UNION ALL
SELECT 10,'Some test user 3' UNION ALL
SELECT 11,'Some test user 4'
DECLARE @Investment TABLE (
Id INT,
Name VARCHAR(50),
OwnerId INT,
ITOwnerId INT,
InformationOwnerId INT,
MaintenanceLeaderId INT,
MaintenanceLeaderITId INT
)
INSERT INTO @Investment(Id,Name,OwnerId,ITOwnerId,InformationOwnerId,MaintenanceLeaderId,MaintenanceLeaderITId)
SELECT 1,'INVESTMENT 1',1,2,1,3,4 UNION ALL
SELECT 2,'INVESTMENT 2',1,3,2,3,2 UNION ALL
SELECT 3,'INVESTMENT 3',3,1,3,3,4 UNION ALL
SELECT 4,'INVESTMENT 4',5,4,4,2,3 UNION ALL
SELECT 5,'INVESTMENT 5',6,5,5,7,6 UNION ALL
SELECT 6,'INVESTMENT 6',8,6,6,7,8 UNION ALL
SELECT 7,'INVESTMENT 7',9,8,7,4,5 UNION ALL
SELECT 8,'INVESTMENT 8',11,8,8,6,11 UNION ALL
SELECT 9,'INVESTMENT 9',10,9,9,10,9
--SELECT * FROM @People
--SELECT * FROM @Investment
-- THIS IS YOUR SELECT STATEMENT to be uses in PetaPoco call
SELECT unpv.Id,unpv.Name, unpv.INVTYPE,unpv.PersonId,p.Name FROM @Investment
UNPIVOT(PersonId for INVTYPE in (OwnerId,ITOwnerId,InformationOwnerId,MaintenanceLeaderId,MaintenanceLeaderITId))unpv
join @People p on unpv.PersonId = p.Id
order by INVTYPE, PersonId
次に、C#コードを次のように
1 - 投資 POCO を拡張して、INVTYPE と PersonId の 2 つの列を追加します。同じ名前空間に新しいオブジェクトを作成するだけです。そのようです:
public partial class Investment
{
[ResultColumn]
public string INVTYPE { set; get; }
[ResultColumn]
public int PersonId { set; get; }
}
2 - ビュークラス InvestmentView を作成します (そのために Investment クラスを使用できます。名前を変更するだけです)
var myInvestmentView = new InvestmentView
{
Id = result.Id,
Name = result.Name,
Owner = new Person{ Id = result.firstOrDefault(o => o.INVTYPE.Equals("OwnerId")).PersonId, Name = result.firstOrDefault(o=> o.INVTYPE.Equals("OwnerId")).PersonName},
ITOwner = new Person{ Id = result.firstOrDefault(o => o.INVTYPE.Equals("ITOwnerId")).PersonId, Name = result.firstOrDefault(o=> o.INVTYPE.Equals("ITOwnerId")).PersonName},
InformationOwner = new Person{ Id = result.firstOrDefault(o => o.INVTYPE.Equals("InformationOwnerId")).PersonId, Name = result.firstOrDefault(o=> o.INVTYPE.Equals("InformationOwnerId")).PersonName},
MaintenanceLeader = new Person{ Id = result.firstOrDefault(o => o.INVTYPE.Equals("MaintenanceLeaderId")).PersonId, Name = result.firstOrDefault(o=> o.INVTYPE.Equals("MaintenanceLeaderId")).PersonName},
MaintenanceLeaderIT = new Person{ Id = result.firstOrDefault(o => o.INVTYPE.Equals("MaintenanceLeaderITId")).PersonId, Name = result.firstOrDefault(o=> o.INVTYPE.Equals("MaintenanceLeaderITId")).PersonName}
}
3 - 返されたリストからのデータをクラスに取り込みます
using (var data = new Database(Config.MainDbConnectionName))
{
var result = data.Fetch<Investment,People>(
Sql.Builder
.Append("SELECT unpv.Id,unpv.Name, unpv.INVTYPE,unpv.PersonId,p.name as PersonName FROM Investment")
.Append(" UNPIVOT(PersonId for INVTYPE in (OwnerId,ITOwnerId,InformationOwnerId,MaintenanceLeaderId,MaintenanceLeaderITId)) unpv")
.Append(" JOIN People p on unpv.PersonId = p.Id")
.Append(" WHERE (Investment.Id = @0)",InvId)
.Append(" ORDER BY INVTYPE, PersonId")
);
}
このように、特別な処理が必要な唯一のクラスは、InvestmentView から POCO のフラット構造にデータを逆方向に処理する必要があるため、Investment クラスになります。