ObservableCollections sans doublons

Résolu/Fermé
danny243 Messages postés 74 Date d'inscription vendredi 19 mars 2021 Statut Membre Dernière intervention 16 octobre 2021 - 12 mai 2021 à 11:04
Whismeril Messages postés 19036 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 2 mai 2024 - 13 mai 2021 à 07:30
Bonjour, actuellement j'ai une base des données qui contient 3 tables et dans mes tables j'ai plusieurs dont le champ année et dans mon application Csharp je manipule cette table .
donc j'aimerai recuperer toutes les années dans mon champ année mais sans doublons et les mettre dans combobox j'essaie de faire de quoi mais je suis bloqué parce que j'avais un biding sauf qu'il m'affiche toutes les années de ma table moi je veux juste les années uniques pouvez vous m'aider
voici mon code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using BLL;

namespace UIL
{
    /// <summary>
    /// Logique d'interaction pour UCSommeVentesProv.xaml
    /// </summary>
    /// 
    /// <summary>
    /// Nom:Daniel Kabeya
    /// Matricule:1812147
    /// </summary>
    public partial class UCSommeVentesProv : UserControl
    {
        List<int> listeAnnee = new List<int>();
        public UCSommeVentesProv()
        {
            InitializeComponent();
          
            

            //cboAnneeDeb.ItemsSource = Vente.vente;
        }

        private void cboAnneeDeb_Loaded(object sender, RoutedEventArgs e)
        {

            foreach (Vente v in Vente.vente)
            {
               
            }
            cboAnneeDeb.ItemsSource = Vente.vente;
        }

        private void cboAnneeDeb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            

           
        }
    }
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.ObjectModel;

using System.Data;
using MySql.Data.MySqlClient;

using DAL;

namespace BLL
{
    /// <summary>
    /// Nom:Daniel Kabeya
    /// Matricule:1812147
    /// </summary>
    public class Vente: INotifyPropertyChanged
    {

        public static ObservableCollection<Vente> vente = new ObservableCollection<Vente>();

       /* public Vente(string province,string genre,int annee,double nbunites, double montantx1000)
        {
            this.province = province;
            this.genre = genre;
            this.annee = annee;
            this.nbunites = nbunites;
            this.montantx1000 = montantx1000;

        }*/


        /// <summary>
        /// Methode qui est de type "void" qui me permet de récuperer les valeurs de chaque lignes de ma table et ensuite les affecter à mes propriétés à la 
        /// fin j'ajoute cela à ma liste 
        /// </summary>
        public static void ChargerListeVente()
        {
            //Création de la collection observable
            var listeVente = new ObservableCollection<Vente>();
            //Appel de la fonction dans AccessDB pour la connection à la DB
            DataTable dt = AccessDB.ConnecterBDVente();

            for (int i = 0; i < dt.Rows.Count; i++)
            {

                var ven = new Vente

                {

                    //Création d'un nouvel objet de type province 
                    LaProvince = new Province(dt.Rows[i]["Province"].ToString()),
                    //Création d'un nouvel objet de type vehicule
                    LesGenres = new Vehicule(dt.Rows[i]["Genre"].ToString()),
                    Annee = Int32.Parse(dt.Rows[i]["Annee"].ToString()),
                    NbUnites = Int32.Parse(dt.Rows[i]["NbUnites"].ToString()),
                    Montantx1000 = Int32.Parse(dt.Rows[i]["Montantx1000"].ToString()),


                };





                listeVente.Add(ven);
            }

            vente = listeVente;
        }
      /*  private string province;
        public string Province 
        {

            get
            {
                return province;
            }
            set
            {
                if (this.province != value)
                {
                    this.province = value;
                    this.NotifyPropertyChanged("Province");
                }
            }
        }*/
      /// <summary>
      /// je crée un attribut de type Province et je crée une propriété avec , qui est aussi de type Province
      /// </summary>
        private Province laProvince;

        public Province LaProvince
        {
            get
            {
                return laProvince;
            }

            set
            {
                if (this.laProvince != value)
                {
                    this.laProvince = value;
                    this.NotifyPropertyChanged("LaProvince");
                }
            }
        }
        /// <summary>
        /// je crée un attribut de type Vehicule et je crée une propriété avec, qui est aussi de type Vehicule
        /// </summary>
        private Vehicule lesGenres;
        public Vehicule LesGenres
        {
            get
            {
                return lesGenres;
            }

            set
            {
                if (this.lesGenres != value)
                {
                    this.lesGenres = value;
                    this.NotifyPropertyChanged("LesGenres");
                }
            }
        }

        private int anneeUnique;

        public int AnneeUnique
        {

            get
            {
                return anneeUnique;
            }
            set
            {
                if (this.anneeUnique != value)
                {
                    this.anneeUnique = value;
                    this.NotifyPropertyChanged("AnneeUnique");
                }
            }
        }



        /// <summary>
        /// je crée un attribut de type string et je crée une propriété avec, qui est aussi de type string
        /// </summary>
        private string genre;
        public string Genre 
        {

            get
            {
                return genre;
            }
            set
            {
                if (this.genre != value)
                {
                    this.genre = value;
                    this.NotifyPropertyChanged("Genre");
                }
            }
        }

        /// <summary>
        /// je crée un attribut de type int et je crée une propriété avec, qui est aussi de type int
        /// </summary>
        private int annee;
        public int Annee 
        
        {

            get
            {
                return annee;
            }
            set
            {
                if (this.annee != value)
                {
                    this.annee = value;
                    this.NotifyPropertyChanged("Annee");
                }
            }


        }

        /// <summary>
        /// je crée un attribut de type int et je crée une propriété avec, qui est aussi de type int
        /// </summary>
        private int nbunites;
        public int NbUnites 
        {


            get
            {
                return nbunites;
            }
            set
            {
                if (this.nbunites != value)
                {
                    this.nbunites = value;
                    this.NotifyPropertyChanged("NbUnites");
                }
            }

        }
        /// <summary>
        /// je crée un attribut de type int et je crée une propriété avec, qui est aussi de type int
        /// </summary>

        private int montantx1000;
        public int Montantx1000 
        {
            get
            {
                return montantx1000;
            }
            set
            {
                if (this.montantx1000 != value)
                {
                    this.montantx1000 = value;
                    this.NotifyPropertyChanged("Montantx1000");
                }
            }
        }
        
        public void NotifyPropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }
}



<UserControl x:Class="UIL.UCSommeVentesProv"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:UIL"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
   
    <UserControl.Resources>
        <Style TargetType="ComboBox">
            <Setter Property="FontSize" Value="16"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="Foreground" Value="Red"/>
            <Setter Property="FontStyle" Value="Italic"/>
        
        </Style>
        <Style TargetType="Button">
            <Setter Property="FontSize" Value="16"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="Foreground" Value="Red"/>
            <Setter Property="FontStyle" Value="Italic"/>
            <Setter Property="Background" Value="Green"/>

        </Style>

    </UserControl.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="19*"/>
            <ColumnDefinition Width="41*"/>
            <ColumnDefinition Width="100*"/>
        </Grid.ColumnDefinitions>
        <Label Content="Label" HorizontalAlignment="Left" Height="60" Margin="23,33,0,0" VerticalAlignment="Top" Width="207" Grid.ColumnSpan="2"/>
        <ListBox Grid.ColumnSpan="2" Grid.Column="1" HorizontalAlignment="Left" Height="186" Margin="89,180,0,0" VerticalAlignment="Top" Width="455">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding NomProvince}"/>

                    </StackPanel>

                </DataTemplate>
            </ListBox.ItemTemplate>


        </ListBox>
        <ComboBox Name="cboAnneeDeb" Grid.Column="1" HorizontalAlignment="Left" Height="32" Margin="89,82,0,0" VerticalAlignment="Top" Width="126" Grid.ColumnSpan="2"  Loaded="cboAnneeDeb_Loaded" SelectionChanged="cboAnneeDeb_SelectionChanged">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Annee}" ></TextBlock>
                </DataTemplate>
            </ComboBox.ItemTemplate>

        </ComboBox>

        <ComboBox Grid.Column="2" HorizontalAlignment="Left" Height="32" Margin="223,82,0,0" VerticalAlignment="Top" Width="116"/>
        <Label Content="Province" Grid.Column="1" HorizontalAlignment="Left" Height="31" Margin="89,144,0,0" VerticalAlignment="Top" Width="75"/>
        <Label Content="Somme en CAD (x1000)" Grid.Column="2" HorizontalAlignment="Left" Height="31" Margin="179,144,0,0" VerticalAlignment="Top" Width="160"/>
        <Button Content="Valider" Grid.Column="2" HorizontalAlignment="Left" Margin="390,220,0,0" VerticalAlignment="Top" Width="80" Height="31"/>
        <ComboBox Grid.Column="2" HorizontalAlignment="Left" Margin="50,82,0,0" VerticalAlignment="Top" Width="124" Height="32"/>

    </Grid>
</UserControl>








Configuration: Windows / Chrome 90.0.4430.212

5 réponses

Whismeril Messages postés 19036 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 2 mai 2024 931
12 mai 2021 à 12:45
Bonjour

Regarde la méthode Linq Distinct.
0
danny243 Messages postés 74 Date d'inscription vendredi 19 mars 2021 Statut Membre Dernière intervention 16 octobre 2021
12 mai 2021 à 20:34
j'ai regardé celà mais je ne comprends pas très bien peux tu m'orienter un peu
0
danny243 Messages postés 74 Date d'inscription vendredi 19 mars 2021 Statut Membre Dernière intervention 16 octobre 2021
12 mai 2021 à 17:18
ah okay d'accord
0
Whismeril Messages postés 19036 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 2 mai 2024 931
12 mai 2021 à 22:40
Dans la méthode qui charge tes ventes, après cette ligne
  vente = listeVente; 

A partir de listVente, tu fais une requête select qui ne récupère que les années, puis une requête distinct qui supprimera les doublons
0
danny243 Messages postés 74 Date d'inscription vendredi 19 mars 2021 Statut Membre Dernière intervention 16 octobre 2021
12 mai 2021 à 22:45
okay d'accord merci
0
danny243 Messages postés 74 Date d'inscription vendredi 19 mars 2021 Statut Membre Dernière intervention 16 octobre 2021
13 mai 2021 à 07:07
il marche merci bien
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Whismeril Messages postés 19036 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 2 mai 2024 931
13 mai 2021 à 07:30
De rien
0