Dapper.NET remplissant une classe dans "list of class"

.net c# dapper sql-server

Question

Je suis nouveau sur Dapper.NET un peu coincé sur ce point. J'essaie de remplir une classe qui a une autre classe de jeu de résultats multiray.

# DATABASE SP>

SELECT b.BuildingId, b.BuildingName, b.Wood, b.Food, b.Stone, b.Gold FROM UserBuildings ub, Buildings b WHERE b.BuildingId = ub.BuildingId AND UserId = @UserId

# CODE>

    using (IDbConnection connection = OpenConnection())
    {
        List<Building.Building> buildings = new List<Building.Building>();

        var multi = connection.QueryMultiple<Building.Building, Resource.Resource>("UserBuildingGet", new { UserId = UserId }, commandType: CommandType.StoredProcedure).ToList();

        building.Resource = multi.Read<Resource.Resource>().Single();

        return building;
    }

# CLASSES>

public class Building
{
    private int _BuildingId;
    private string _BuildingName;
    private Resource.Resource _Resource;

public int BuildingId
{
    get { return _BuildingId; }
    set { _BuildingId = value; }
}
public string BuildingName
{
    get { return _BuildingName; }
    set { _BuildingName = value; }
}
public Resource.Resource Resource
{
    get { return _Resource; }
    set { _Resource = value; }
}

public Building(int BuildingId, string BuildingName, Resource.Resource Resource)
{
    this.BuildingId = BuildingId;
    this.BuildingName = BuildingName;
    this.Resource = Resource;
}
}

public class Resource
{
    private int _Wood;
    private int _Food;
    private int _Stone;
    private int _Gold;

public int Wood
{
    get { return _Wood; }
    set { _Wood = value; }
}
public int Food
{
    get { return _Food; }
    set { _Food = value; }
}
public int Stone
{
    get { return _Stone; }
    set { _Stone = value; }
}
public int Gold
{
    get { return _Gold; }
    set { _Gold = value; }
}

public Resource(int Wood, int Food, int Stone, int Gold)
{
    this.Wood = Wood;
    this.Food = Food;
    this.Stone = Stone;
    this.Gold = Gold;
}

}

Réponse acceptée

Votre code doit définir ce qui sépare les données. Utilisez le splitOn paramètre de GridReader.Read

var buildings = new List<Building.Building>();

using (IDbConnection connection = OpenConnection())
{
    using(var reader = connection.QueryMultiple("UserBuildingGet",
                                                new { UserId = UserId },
                                                commandType: CommandType.StoredProcedure))
    {
        var building = reader.Read<Building.Building,
                                   Resource.Resource,
                                   Building.Building>
            ((b, r) => { b.Resource = r; return b; }, splitOn: "Wood");

        buildings.AddRange(building);
    }
}

return buildings;

Voir: Existe - t-il un moyen d’utiliser MultiMapping et QueryMultiple dans Dapper?

Échantillon:

public class Building
{
    public int BuildingId { get; set; }
    public string BuildingName { get; set; }
    public Resource Resource { get; set; }

    public override string ToString()
    {
        return string.Format("Id: {0} Name: {1} Resource: {2}", BuildingId, BuildingName, Resource);
    }
}

public class Resource
{
    public int Wood { get; set; }
    public int Food { get; set; }
    public int Stone { get; set; }
    public int Gold { get; set; }

    public override string ToString()
    {
        return string.Format("Wood: {0} Food: {1} Stone {2} Gold {3}", Wood, Food, Stone, Gold);
    }
}

var sql = @"SELECT 1 AS BuildingId, 'tower' AS BuildingName, 1 AS Wood, 1 AS Food, 1 AS Stone, 1 AS Gold
            UNION ALL
            SELECT 2 AS BuildingId, 'shed' AS BuildingName, 1 AS Wood, 1 AS Food, 1 AS Stone, 1 AS Gold";

var buildings = new List<Building>();

using(var connection = GetOpenConnection())
{
    using(var reader = connection.QueryMultiple(sql))
    {
        var building = reader.Read<Building, Resource, Building>(
            (b, r) => { b.Resource = r; return b; }, splitOn: "Wood");
        buildings.AddRange(building);
    }
}

foreach(var building in buildings)
{
    Console.WriteLine(building);
}


Sous licence: CC-BY-SA with attribution
Non affilié à Stack Overflow
Est-ce KB légal? Oui, apprenez pourquoi
Sous licence: CC-BY-SA with attribution
Non affilié à Stack Overflow
Est-ce KB légal? Oui, apprenez pourquoi