最初の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
は面倒に見えますか?