How to LINQ a List<T>
-
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()
-
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()
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 typoJust 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
-
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 typoJust 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
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"?.
-
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"?.
-
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
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(); -
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(); -
yaa... got it but some other way
return (
from cmd in cmds
where cmd.KeyNum = KeyNum
select cmd.execute()
).ToString();