-1

Hi I have a array list if type class "DtContract".

ArrayList listOfContracts_;

foreach (DTContract contract in listOfContracts_)
{
    if (contract.Engine != DTIsland.EngineType.AMADEUS && contract.Engine !=DTIsland.EngineType.SABRE)
                    continue; 
}

I want to do it through LINQ. I want to filter the Contract whose EngineType == AMADEUS && EngineType == SABRE. Please suggest how can i do it through Linq and get the result in List or in array list.

I am doing this to Optimize the code.

Please Help...

Adriano Carneiro
  • 53,285
  • 12
  • 85
  • 120
Shivi
  • 1,047
  • 9
  • 23
  • 41
  • 1
    Do you want to optimize for speed or for readability? Because converting this `foreach` loop to a Linq query won't improve the speed. – Elian Ebbing May 06 '11 at 14:22
  • @Elian : So how can i improve it?? Please Suggest.. – Shivi May 06 '11 at 14:31
  • Your are missing the constructor call and `listOfContracts_.add(contract)` in your code. What Elian ment is, that linq will internally perform the same, your currently do, and that there is nothing to optimize, from a performance perspective). – Nappy May 06 '11 at 14:47
  • @Nappy: actually it just an example so i dont wright listOfContracts_.add(contract) – Shivi May 06 '11 at 15:06

2 Answers2

1
var result = listOfContracts_.Where(contract=>contract.Engine != DTIsland.EngineType.AMADEUS && contract.Engine !=DTIsland.EngineType.SABRE).ToList();
Ralph Shillington
  • 19,493
  • 20
  • 85
  • 150
0

your foreach loop doen't do anything meaningful, what you are trying achieve?

If you want to use linq

listOfContracts_.OfType<DTContract>()
   .Where(contract => contract.Engine != DTIsland.EngineType.AMADEUS && 
                      contract.Engine != DTIsland.EngineType.SABRE);
Nasmi Sabeer
  • 1,360
  • 9
  • 21