【问题标题】:How to use automapper in dapper interface(IDBConnection) with SQL queries如何通过 SQL 查询在 dapper 接口(IDBConnection)中使用 automapper
【发布时间】:2017-02-21 05:48:14
【问题描述】:

public IEnumerable<voucher> index(string word, string DivisionCode, string yearlabel, int pageno)
        {
            using (IDbConnection dbConnection = Connection)
            {
                var data = dbConnection.Query<fydates>(@"SELECT REPLACE(CONVERT(NVARCHAR(10),fystart,6),' ','-') as fystart,
                REPLACE(CONVERT(NVARCHAR(10),fyend,6),' ','-') as fyend FROM fydates WHERE yearlabel = @yearlabel",
                new { yearlabel = yearlabel }).Single();

                List<voucher> DataQuery = dbConnection.Query<voucher, accounts, division, LockStatus, voucher>(@"SELECT V.id,V.voucherno,
                CASE WHEN V.vouchertype='P' THEN 'Paid' ELSE 'Receipt' END AS vouchertype,
                REPLACE(CONVERT(NVARCHAR(10),V.dated,6),' ','-') AS dated,V.subno,V.amount,COALESCE(V.syscreated,' ') AS syscreated,V.bankcode,
                COALESCE(V.cheqno,' ') AS Cheqno,V.Cheqdate,V.posted,V.currency,A.id as accountsid,A.achead,A.shortname,
                D.id as divisionid,D.divname,D.divncode,L.id as lockstatusid,L.name FROM voucher V
                LEFT JOIN Accounts A ON V.bankcode = A.mainac
                LEFT JOIN divisionmst D ON V.divisionid = D.divncode
                LEFT JOIN LockStatus L ON V.posted=L.Type
                WHERE (V.voucherno + V.Subno+V.cheqno + A.achead + LEFT(yearvoucherno,3) LIKE @word AND
                D.divncode LIKE @DivisionCode) AND (dated BETWEEN @fystart AND @fyend OR dated='31-Mar-49')
                Order By V.id DESC OFFSET @pageno *25 ROWS FETCH NEXT 25 ROWS only",
                (voucher, accounts, division, LockStatus) =>
                {
                    voucher.accounts = accounts;
                    voucher.division = division;
                    voucher.LockStatus = LockStatus;
                    return voucher;
                },
                new { word = word, fystart = data.fystart, fyend = data.fyend, DivisionCode = DivisionCode, pageno = pageno - 1 },
                splitOn: "accountsid, divisionid, lockstatusid").ToList();
                return DataQuery;
            }
        }

在较早的场景中,我在 JOINS 查询中使用了 spliton 映射技术,它非常有效。但是在联合查询或条件的情况下,我被卡住了,因为我无法决定我应该在哪里拆分。如上所述,当条件我无法决定在哪里拆分时,联合和案例

【问题讨论】:

    标签: c# asp.net-mvc .net-core dapper


    【解决方案1】:

    只需添加虚拟列即可帮助 Dapper 知道在哪里拆分。请注意,如果虚拟列为空,则关联对象将为空。另请注意,您的目标对象上不需要存在虚拟列。

    select
    --first object
    1 AS Id,
    (1+1) AS APropertyAssociatedToYourFirstObject,
    (1+1) AS ADifferentPropertyAssociatedToYourFirstObject,
    
    --second object (assuming you are splitting on Id)
    1 AS Id,
    (1+1) AS APropertyAssociatedToYourSecondObject,
    (1+1) AS ADifferentPropertyAssociatedToYourSecondObject
    from YourTable
    

    另外,这样排列可以节省大量时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-10
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      相关资源
      最近更新 更多