IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Linq Discussion :

Method avec list et linq to sql


Sujet :

Linq

  1. #1
    Membre averti Avatar de solaar
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    607
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2007
    Messages : 607
    Points : 314
    Points
    314
    Par défaut Method avec list et linq to sql
    Bonjour.
    j'ai des difficultes avec la methode List. comment ramener la valeur List.
    je comprend pas bien.
    je vous donne mon code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
     
     public class northwindService : InorthwindService
        {
            northwindEntities db = new northwindEntities();
     
            public List<MonProduct> GetAllProductByCathegorie()
            {
     
                var ListProduct = from p in db.Products
                                  where p.Category.CategoryID == p.CategoryID
                                  select new MonProduct
                                  {
                                     ProduitId =p.ProductID,
                                     ProduiName= p.ProductName                                                         
                                  };
     
                return ListProduct.ToList();
     
            }
        }
     
     
    class MonProduct
        {
           public int ProduitId { get; set; }
           public string ProduiName { get; set; }
        }
    je sais bien utilise les autres methode public string/int/bool qui te ramene (return valeur string/int ot bool).
    Mais j'ai des difficulte avec List surtout avec linq to sql.
    je sais meme pas comment je vais appeler ma methode GetAllProductByCathegorie dans mon code.
    j'ai besoin d'explication ou d'un tutorial pour sa merci.


    Cordialement
    solaar

  2. #2
    Membre éclairé Avatar de chamamo
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    588
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 588
    Points : 735
    Points
    735
    Par défaut
    Je vai essayer de répondre même si je ne asis pas ce que tu veux, tu veux que ta méthode retourne autre chose que List?

    tu peux retourner un IQueryable<MonProduct>.

    Et pourquoi tu utilises une autre classe ? (MonProduct)

    tu peux directement retourner un IQueryable<Product> sans passer par ta classe MonProduct.

    Autre chose, ton critère
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    where p.Category.CategoryID == p.CategoryID
    ne sert à rien car il n'y a aucun filtre, ton critère tu dois le passer en paramètre:

    ta méthode devient :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    public IQueryable<Product> GetAllProductByCathegorie(int categoryID)
            {
     
                return from p in db.Products
                                  where p.CategoryID == categoryID
                                  select p;
     
            }

  3. #3
    Membre averti Avatar de solaar
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    607
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2007
    Messages : 607
    Points : 314
    Points
    314
    Par défaut
    Merci c'est ce que je voulais sa ma permis d’améliorer mon code, après explication
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
     
          public IEnumerable<MonProduct> GetAllProductByCathegorie (int IdCatg) 
            {
                var produits =( from p in db.Products
                                where p.CategoryID==IdCatg
                                select new MonProduct
                               {
                                  IDProd = p.ProductID,
                                  NameProd = p.ProductName,
                                  QuantityPerUnitProd = p.QuantityPerUnit,
                                  UnitPriceProd =(int)p.UnitPrice,
                                  UnitsInStockProd =(int)p.UnitsInStock
                               });
                List<MonProduct> Result = new List<MonProduct>(produits);
     
                return Result;
            }
    je travail avec un web service ....
    je veux appeler ma méthode mais je sais pas comment mis prendre.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
     [OperationContract]
    IEnumerable<MonProduct> GetAllProductByCathegorie (int IdCatg) ?????

  4. #4
    Membre éclairé Avatar de chamamo
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    588
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 588
    Points : 735
    Points
    735
    Par défaut
    En appelant la méthode de ton Service:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    var service = new northwindService();
    return service.GetAllProductByCathegorie(IdCatg);

  5. #5
    Membre averti Avatar de solaar
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    607
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2007
    Messages : 607
    Points : 314
    Points
    314
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
     
      [ServiceContract]
        public interface InorthwindService
        {
     
     
            [OperationContract]
     
            List<MonProduct> GetAllProductByCathegorie (int IdCatg) ;
     
            [OperationContract]
            List<MaCathegorie> GetAllCathegorie();
     
         }
     
    /////////////////////////////////
        public class northwindService : InorthwindService
        {
            northwindEntities db = new northwindEntities();
     
     
            public IEnumerable<MonProduct> GetAllProductByCathegorie (int IdCatg) 
            {
                var produits =( from p in db.Products
                                where p.CategoryID==IdCatg
                                select new MonProduct
                               {
                                  IDProd = p.ProductID,
                                  NameProd = p.ProductName,
                                  QuantityPerUnitProd = p.QuantityPerUnit,
                                  UnitPriceProd =(int)p.UnitPrice,
                                  UnitsInStockProd =(int)p.UnitsInStock
                               });
                List<MonProduct> Result = new List<MonProduct>(produits);
     
                return Result;
            }
     
            public IEnumerable<MaCathegorie> GetAllCathegorie()
            { 
              var cathegories=(from p in db.Categories
                               select new MaCathegorie
                               {
                                    IdCathegorie=p.CategoryID,
                                    DescriptionCathegorie=p.CategoryName
     
                               });
                List<MaCathegorie> Result = new List<MaCathegorie>(cathegories);
     
                return Result;
     
            }
     
     
     
            }
    il me donne meme pas la possibilite de faire un VAR .... je suis nouveau j'ai besoin d'explication.

  6. #6
    Membre éclairé Avatar de chamamo
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    588
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 588
    Points : 735
    Points
    735
    Par défaut
    Il faut que tu sois plus clair que ça, qui ne tonne pas la possibilité de faire un var? tu l'as bien utilisé non? c'est quoi l'erreur? tu peux remplacer le var par le type de la variable.

  7. #7
    Membre averti Avatar de solaar
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    607
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2007
    Messages : 607
    Points : 314
    Points
    314
    Par défaut
    Je veux mettre les méthodes dans mon Interface InorthwindService :
    puis après les appeler lors de mon Développent sur mes formulaire ou UserCntrol.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
      [ServiceContract]
        public interface InorthwindService
        {
     
     
            [OperationContract]
              List<MonProduct> GetAllProductByCathegorie (int IdCatg) ;
     
            [OperationContract]
            List<MaCathegorie> GetAllCathegorie();
     
         }

  8. #8
    Membre averti Avatar de solaar
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    607
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2007
    Messages : 607
    Points : 314
    Points
    314
    Par défaut
    je montre l'erreur :
    Car j'ai pas déclare de méthodes.
    Images attachées Images attachées  

  9. #9
    Membre averti Avatar de solaar
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    607
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2007
    Messages : 607
    Points : 314
    Points
    314
    Par défaut
    c'est bon j'ai trouve Merci encore Chamamo
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
      [ServiceContract]
        public interface InorthwindService
        {
     
     
            [OperationContract]
            IEnumerable<MonProduct> GetAllProductByCathegorie(int IdCatg);
     
            [OperationContract]
            IEnumerable<MaCathegorie> GetAllCathegorie();
     
         }

  10. #10
    Membre éclairé Avatar de chamamo
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    588
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 588
    Points : 735
    Points
    735
    Par défaut
    Bon dév, merci de marquer le topic comme résolu

  11. #11
    Membre averti Avatar de solaar
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    607
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Côte d'Ivoire

    Informations professionnelles :
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2007
    Messages : 607
    Points : 314
    Points
    314
    Par défaut Ma methode ne ramene aucune valeur.
    Bonjour .
    je comprend plus rien de ma method.
    je une table Departement avec "region" comme cle secondaire.
    je fait un trie sur le libelle department;

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
     
           public static IEnumerable<DEPARTEMENT> GetDepartementQuery(string dept , DomainContext _db)
            {
     
                return (from q in _db.DEPARTEMENTs where q.LIBELLE_DEPT == dept select q).ToList();
            }
    il ne me ramene aucune valeur.... je sais vraiment pas. .... je travail avec DomaineService.



    Merci pour l'aide

Discussions similaires

  1. Deploiement MSI avec projet utilisant linq to sql
    Par spezet29 dans le forum VB.NET
    Réponses: 0
    Dernier message: 05/07/2010, 10h34
  2. Trier une liste avec LINQ to SQL
    Par anthride dans le forum Linq
    Réponses: 2
    Dernier message: 12/02/2010, 10h40
  3. Problème avec les méthodes Linq to SQL en C#
    Par Lennox dans le forum Silverlight
    Réponses: 7
    Dernier message: 05/09/2008, 18h53
  4. Method invoke avec List<generics>
    Par youx21 dans le forum Langage
    Réponses: 4
    Dernier message: 06/12/2007, 09h11
  5. [SQL] Problème avec liste déroulante et select
    Par cari dans le forum PHP & Base de données
    Réponses: 15
    Dernier message: 28/07/2006, 20h52

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo