3

最初のDapper.Netプロジェクトを実装しています。今、別のオブジェクト(マルチマッピング)を含むオブジェクトを初期化する最も簡単な方法は何だろうと思っています。

これが私のコードです:

public static IEnumerable<ShopPrefix> GetShopPrefixes(short fiSL)
{
    using (var con = new SqlConnection(Properties.Settings.Default.RM2Con))
    {
        const String sql = @"
            SELECT  locShopPrefix.idShopPrefix,
                    locShopPrefix.fiSL,
                    locShopPrefix.fiShop,
                    locShopPrefix.Prefix,
                    locShopPrefix.Active,
                    locShop.idShop,
                    locShop.ShopName,
                    locShop.ContactPerson,
                    locShop.Street,
                    locShop.ZIP,
                    locShop.City,
                    locShop.Telephone,
                    locShop.Telefax,
                    locShop.Email,
                    locShop.ShopKey
            FROM    locShopPrefix
                    INNER JOIN locShop
                        ON locShopPrefix.fiShop = locShop.idShop
            WHERE   (locShopPrefix.fiSL = @fiSL);";
        con.Open();
        IEnumerable<Tuple<ShopPrefix,Shop>> shops =
            con.Query<ShopPrefix, Shop, Tuple<ShopPrefix, Shop>>(
            sql
            , (shopPrefix, shop) => Tuple.Create(shopPrefix, shop)
            , new { fiSL = fiSL }, splitOn: "idShop"
        );
        foreach (var shop in shops)
            shop.Item1.Shop = shop.Item2;
        return shops.Select(t => t.Item1);
    }
}

したがって、 everyshopPrefixは a に属します (持っています) Shop

Q:これは 2 つのオブジェクトをマップする正しい方法ですか?Tuple次のようforeachにプロパティを初期化する方法Shopは面倒に見えますか?

4

1 に答える 1

4

IEnumerable<Tuple<>>単純な 1 対 1 のオブジェクト関係には必要ないと思います。次のスニペットでも十分です。

var shopsPrefixes = con.Query<ShopPrefix, Shop, ShopPrefix>(sql
    , (shopPrefix, shop) =>
    {
        shopPrefix.Shop = shop;
        return shopPrefix;
    }
    , new { fiSL = fiSL }
    , splitOn: "idShop"
);
return shopsPrefixes;
于 2012-07-17T16:45:17.777 に答える