Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. How to LINQ a List<T>

How to LINQ a List<T>

Scheduled Pinned Locked Moved LINQ
csharphelplinqdata-structuresregex
7 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    prashantpatole
    wrote on last edited by
    #1

    I am new to .Net 3x, and trying out few things in LINQ, I was trying to implement 'Command' pattern in .Net 3 for the said purpose. But i am stuck with how to use LINQ to search through List (a List of interfaceed object) Here is the code

    //main control pannel class
    class RemoteControl
    {
    List <iCommand> cmds = new List<iCommand>(); //I know this can be simple if i use simple [] array but only to chekout LINQ feature with collections.....

        Fan f = new Fan("main fan");
        Fan ef = new Fan("exaust fan");
        Light l = new Light("Hall");
        Light lD = new Light("Disco");
        MusicSystem ms = new MusicSystem("Disk");
    
    
        public RemoteControl()
        {
            cmds.Add(new FanOnCommand(f,1));
            cmds.Add(new FanOffCommand(f,2));
            //cmds.Add(new FanIncreeseCommand(f,3));
            //cmds.Add(new FanDeccreeseCommand(f,4));
            for (int i = 2; i <= 10; i++)
            {
                cmds.Add(new NoCommand(new NoDevice(),i+1));
            }
        }
    
        public string PressKey(int KeyNum)
        {
            if (1 < KeyNum && KeyNum < 11)
            {
    
                return (
                    from cmd in cmds
                    where cmd.KeyNum = KeyNum  ///PROBLEM AREA
                    select cmd
                    ).execute();
                
            }
            else
            {
                return "invalid button";
            }
    
        }
    
    }
    

    //commands code

    public interface iCommand 
    {
        int getKeyNum();
        string execute();
    }
    
    
    
    #region "Light Commands"
    class  LightOnCommand 
        : iCommand
       {
        Light \_light;
        public int \_keyNum;
    
        public LightOnCommand(Light light, int keyNumber)
        {
            \_keyNum = keyNumber;
            \_light = light;
        }
    
         public string execute()
        {
            return \_light.LightOn();
        }
    
        public int getKeyNum()
        {
            return \_keyNum;
        }
    }
    
    class LightOffCommand : iCommand
    {
    
        Light \_light;
        public int \_keyNum;
    
        public LightOffCommand(Light light, int keyNumber)
        {
            \_keyNum = keyNumber;
            \_light = light;
        }
    
          public string execute()
        {
            return \_light.LightOff();
        }
        public int getKeyNum()
    
    D 1 Reply Last reply
    0
    • P prashantpatole

      I am new to .Net 3x, and trying out few things in LINQ, I was trying to implement 'Command' pattern in .Net 3 for the said purpose. But i am stuck with how to use LINQ to search through List (a List of interfaceed object) Here is the code

      //main control pannel class
      class RemoteControl
      {
      List <iCommand> cmds = new List<iCommand>(); //I know this can be simple if i use simple [] array but only to chekout LINQ feature with collections.....

          Fan f = new Fan("main fan");
          Fan ef = new Fan("exaust fan");
          Light l = new Light("Hall");
          Light lD = new Light("Disco");
          MusicSystem ms = new MusicSystem("Disk");
      
      
          public RemoteControl()
          {
              cmds.Add(new FanOnCommand(f,1));
              cmds.Add(new FanOffCommand(f,2));
              //cmds.Add(new FanIncreeseCommand(f,3));
              //cmds.Add(new FanDeccreeseCommand(f,4));
              for (int i = 2; i <= 10; i++)
              {
                  cmds.Add(new NoCommand(new NoDevice(),i+1));
              }
          }
      
          public string PressKey(int KeyNum)
          {
              if (1 < KeyNum && KeyNum < 11)
              {
      
                  return (
                      from cmd in cmds
                      where cmd.KeyNum = KeyNum  ///PROBLEM AREA
                      select cmd
                      ).execute();
                  
              }
              else
              {
                  return "invalid button";
              }
      
          }
      
      }
      

      //commands code

      public interface iCommand 
      {
          int getKeyNum();
          string execute();
      }
      
      
      
      #region "Light Commands"
      class  LightOnCommand 
          : iCommand
         {
          Light \_light;
          public int \_keyNum;
      
          public LightOnCommand(Light light, int keyNumber)
          {
              \_keyNum = keyNumber;
              \_light = light;
          }
      
           public string execute()
          {
              return \_light.LightOn();
          }
      
          public int getKeyNum()
          {
              return \_keyNum;
          }
      }
      
      class LightOffCommand : iCommand
      {
      
          Light \_light;
          public int \_keyNum;
      
          public LightOffCommand(Light light, int keyNumber)
          {
              \_keyNum = keyNumber;
              \_light = light;
          }
      
            public string execute()
          {
              return \_light.LightOff();
          }
          public int getKeyNum()
      
      D Offline
      D Offline
      Dan Mos
      wrote on last edited by
      #2

      Hy, You need to expose it as a Property => the KeyNum. I see that you have a Java background or something cause you are not using properties but functions. Insted of

      public int getKeyNum()
      {
      return _keyNum;
      }

      //use

      public int KeyNum{
      get{return _keyNum;} //or something like that
      }

      That is a general ideea/remark not just for this one. also I noticed :

      where cmd.KeyNum = KeyNum //PROBLEM AREA
      //sould be
      where cmd.KeyNum == KeyNum//but this could just as well be a typo

      Just some random remarks cause your question is not very clear at least for me. :)

      Just an irritated, ranting son of ... an IT guy. At your trolling services

      P 1 Reply Last reply
      0
      • D Dan Mos

        Hy, You need to expose it as a Property => the KeyNum. I see that you have a Java background or something cause you are not using properties but functions. Insted of

        public int getKeyNum()
        {
        return _keyNum;
        }

        //use

        public int KeyNum{
        get{return _keyNum;} //or something like that
        }

        That is a general ideea/remark not just for this one. also I noticed :

        where cmd.KeyNum = KeyNum //PROBLEM AREA
        //sould be
        where cmd.KeyNum == KeyNum//but this could just as well be a typo

        Just some random remarks cause your question is not very clear at least for me. :)

        Just an irritated, ranting son of ... an IT guy. At your trolling services

        P Offline
        P Offline
        prashantpatole
        wrote on last edited by
        #3

        oops.. i guess it must be "==" The problem area is indicated, only to help people get started from a point. thanks a lot buddy, i ll try that out u sure if it is possible to access tht property even if the "cmd" object is an instance of an interface "iCommand"?.

        D 1 Reply Last reply
        0
        • P prashantpatole

          oops.. i guess it must be "==" The problem area is indicated, only to help people get started from a point. thanks a lot buddy, i ll try that out u sure if it is possible to access tht property even if the "cmd" object is an instance of an interface "iCommand"?.

          D Offline
          D Offline
          Dan Mos
          wrote on last edited by
          #4

          Yes. It will work. As long as you have it in the interface too. That is make the interface expose the property instead of the method/function.

          Just an irritated, ranting son of ... an IT guy. At your trolling services

          P 1 Reply Last reply
          0
          • D Dan Mos

            Yes. It will work. As long as you have it in the interface too. That is make the interface expose the property instead of the method/function.

            Just an irritated, ranting son of ... an IT guy. At your trolling services

            P Offline
            P Offline
            prashantpatole
            wrote on last edited by
            #5

            thanks a lot for your suggesstion dear.. i really helped but as i was scared same thing happened. i think some TYpe Casting is needed here i got this error at very next line on "}.execute()" Error 1 'System.Collections.Generic.IEnumerable<MyFirstWPF.Model.iCommand>' does not contain a definition for 'execute' and no extension method 'execute' accepting a first argument of type 'System.Collections.Generic.IEnumerable<MyFirstWPF.Model.iCommand>' could be found (are you missing a using directive or an assembly reference?) C:\Users\Shubee--V\Documents\Visual Studio 2008\Projects\MyFirstWPF\MyFirstWPF\Model\RemoteControl.cs 41 22 MyFirstWPF

            return (
            from cmd in cmds
            where cmd.KeyNum = KeyNum
            select cmd
            ).execute();

            J 1 Reply Last reply
            0
            • P prashantpatole

              thanks a lot for your suggesstion dear.. i really helped but as i was scared same thing happened. i think some TYpe Casting is needed here i got this error at very next line on "}.execute()" Error 1 'System.Collections.Generic.IEnumerable<MyFirstWPF.Model.iCommand>' does not contain a definition for 'execute' and no extension method 'execute' accepting a first argument of type 'System.Collections.Generic.IEnumerable<MyFirstWPF.Model.iCommand>' could be found (are you missing a using directive or an assembly reference?) C:\Users\Shubee--V\Documents\Visual Studio 2008\Projects\MyFirstWPF\MyFirstWPF\Model\RemoteControl.cs 41 22 MyFirstWPF

              return (
              from cmd in cmds
              where cmd.KeyNum = KeyNum
              select cmd
              ).execute();

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              return (
              from cmd in cmds
              where cmd.KeyNum = KeyNum
              select cmd
              ).First().execute();

              P 1 Reply Last reply
              0
              • J J4amieC

                return (
                from cmd in cmds
                where cmd.KeyNum = KeyNum
                select cmd
                ).First().execute();

                P Offline
                P Offline
                prashantpatole
                wrote on last edited by
                #7

                yaa... got it but some other way

                return (
                from cmd in cmds
                where cmd.KeyNum = KeyNum
                select cmd.execute()
                ).ToString();

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups