public static EpreuveCombatModel ToModel(this EpreuveCombat dto)
        {
            EpreuveCombatModel result = new EpreuveCombatModel();

            result.Id               = dto.Id;
            result.Nom              = dto.Nom;
            result.CategorieId      = dto.CategoriePratiquantId;
            result.GenreCategorieId = (int)dto.GenreCategorie;
            result.GradeAutoriseId  = (int)dto.GradeAutorise;
            result.StatutId         = (int)dto.Statut;
            result.TypeEpreuveId    = dto.TypeEpreuveId;
            result.PoidsMini        = dto.PoidsMini;
            result.PoidsMaxi        = dto.PoidsMaxi;
            result.IsMerged         = dto.IsMerged;
            return(result);
        }
        public JsonResult MergeEpreuve(IEnumerable <EpreuveModel> epreuves, string areTechs)
        {
            bool techEpreuve;

            if (bool.TryParse(areTechs, out techEpreuve))
            {
                try
                {
                    var            result  = new JsonResult();
                    List <Epreuve> sources = new List <Epreuve>();
                    if (techEpreuve)
                    {
                        var matches = (from epreuve in epreuves
                                       from dbItem in this.epreuvestechniquesRepository.Read()
                                       where dbItem.Id == epreuve.EpreuveId
                                       select dbItem).ToList();

                        foreach (var item in matches)
                        {
                            sources.Add(item);
                            item.Statut = StatutEpreuve.Exclue;
                            this.epreuvestechniquesRepository.Update(item);
                        }
                    }
                    else
                    {
                        var matches = (from epreuve in epreuves
                                       from dbItem in this.epreuvesCombatRepository.Read()
                                       where dbItem.Id == epreuve.EpreuveId
                                       select dbItem).ToList();

                        foreach (var item in matches)
                        {
                            sources.Add(item);
                            item.Statut = StatutEpreuve.Exclue;
                            this.epreuvesCombatRepository.Update(item);
                        }
                    }

                    sources.OrderByDescending(m => m.CategoriePratiquantId);

                    var categoryNames = (from cat in this.unitOfWork.Repository <CategoriePratiquant>().Read()
                                         join model in sources on cat.Id equals model.CategoriePratiquantId
                                         select cat.Nom).Distinct();
                    Epreuve merged;
                    if (techEpreuve)
                    {
                        merged = new EpreuveTechnique();
                    }
                    else
                    {
                        merged = new EpreuveCombat();
                    }

                    merged.IsMerged = true;
                    merged.CategoriePratiquantId = sources.First().CategoriePratiquantId;
                    var isGenreMerged = sources.GroupBy(m => m.GenreCategorie).Distinct().Count() > 1;
                    if (isGenreMerged)
                    {
                        merged.GenreCategorie = GenreEpreuve.Mixte;
                    }
                    else
                    {
                        merged.GenreCategorie = sources.First().GenreCategorie;
                    }

                    merged.GradeAutorise = mergeGrades(sources);
                    merged.Statut        = StatutEpreuve.Ouverte;
                    merged.TypeEpreuveId = sources.First().TypeEpreuveId;

                    var genre       = GenreEpreuves.ResourceManager.GetString(merged.GenreCategorie.ToString());
                    var grade       = Grades.ResourceManager.GetString(merged.GradeAutorise.ToString());
                    var typeEpreuve = this.typesEpreuvesRepository.Read(sources.First().TypeEpreuveId).Nom;
                    merged.Nom = string.Format("{0} {1} {2} {3}", typeEpreuve, string.Join(" ", categoryNames.ToArray()), genre, grade);
                    if (!techEpreuve)
                    {
                        ((EpreuveCombat)merged).PoidsMaxi = sources.Cast <EpreuveCombat>().Max(e => e.PoidsMaxi);
                        ((EpreuveCombat)merged).PoidsMini = sources.Cast <EpreuveCombat>().Min(e => e.PoidsMini);
                        merged.Nom = BuildEpreuveName(sources.First().CategoriePratiquant.Nom, typeEpreuve, genre, grade, ((EpreuveCombat)merged).PoidsMini, ((EpreuveCombat)merged).PoidsMaxi);

                        //merged.Nom = string.Format("{0} de {1}kgs à {2}kgs", merged.Nom, ((EpreuveCombat)merged).PoidsMini, ((EpreuveCombat)merged).PoidsMaxi);
                    }

                    if (techEpreuve)
                    {
                        this.epreuvestechniquesRepository.Create(merged as EpreuveTechnique);
                    }
                    else
                    {
                        this.epreuvesCombatRepository.Create(merged as EpreuveCombat);
                    }

                    var idForNewEpreuve = merged.Id;

                    var partipations = this.unitOfWork.Repository <Participation>();

                    var participationsToChange = (from participation in partipations.Read()
                                                  join model in sources
                                                  on participation.EpreuveId equals model.Id
                                                  select participation).ToList();

                    for (int i = 0; i < participationsToChange.Count(); i++)
                    {
                        participationsToChange[i].EpreuveId = idForNewEpreuve;
                        partipations.Update(participationsToChange[i]);
                    }

                    result.Data = string.Empty;
                    return(result);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            else
            {
                throw new ArgumentException("Passed parameter can't be parsed to bool type", "areTechs");
            }
        }